Why Zend_View_Helper_Translate->translate() returns $this
Posted: Fri Feb 26, 2010 6:14 pm
After debugging the "cannot use object of class Zend_View_Helper_Translate as a string" error, I'm trying to understand what's the intension of returning the instance, when null is passed. OK, I know in ZF when you have setter you are returning $this, so you can chain function calls, but that's not a setter, and you are expecting to receive a string. And if you use this in concatenation, as in some Digitalus' view scripts, you receive this catchable fatal error. So, am I missing some hidden reason for returning $this? Or this is more likely bug in the zend framework? why would you chain calls, using a method that does not change anything, and only returns $this?
Here is the code... it's from Zend Framework 1.10 (link to api documentation)
Here is the code... it's from Zend Framework 1.10 (link to api documentation)
Code: Select all
public function translate($messageid = null)
{
if ($messageid === null) {
return $this;
}
$translate = $this->getTranslator();
$options = func_get_args();
array_shift($options);
$count = count($options);
$locale = null;
if ($count > 0) {
if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
$locale = array_pop($options);
}
}
if ((count($options) === 1) and (is_array($options[0]) === true)) {
$options = $options[0];
}
if ($translate !== null) {
$messageid = $translate->translate($messageid, $locale);
}
if (count($options) === 0) {
return $messageid;
}
return vsprintf($messageid, $options);
}