Page 1 of 1

Why Zend_View_Helper_Translate->translate() returns $this

Posted: Fri Feb 26, 2010 6:14 pm
by Darhazer
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)

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);
    }

Re: Why Zend_View_Helper_Translate->translate() returns $this

Posted: Fri Feb 26, 2010 6:21 pm
by Eran
When returning $this, it's usually for a fluent interface. Looking in the reference manual, that appears to be the case here -
There are several ways to initiate the Translate View Helper:
* Registered, through a previously registered instance in Zend_Registry
* Afterwards, through the fluent interface
* Directly, through initiating the class
http://framework.zend.com/manual/en/zen ... lpers.html

Re: Why Zend_View_Helper_Translate->translate() returns $this

Posted: Fri Feb 26, 2010 6:29 pm
by Darhazer
Yeah, but returning either an object or not an object makes chaining dangerous. And the point of using fluent interface is chaining calls, isn't it? Why would you even call translate() just to receive the same instance, without changing / performing anything?

Re: Why Zend_View_Helper_Translate->translate() returns $this

Posted: Fri Feb 26, 2010 8:02 pm
by Eran
Translate is a view method which instantiates the helper. They were probably trying to accommodate several configuration styles, such as the one shown in the manual:

Code: Select all

$adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
$this->translate()->setTranslator($adapter)->translate('simple');

Re: Why Zend_View_Helper_Translate->translate() returns $this

Posted: Sat Feb 27, 2010 3:51 am
by Darhazer
Now I see
Thanks pytrin
Wouldn't it be better, if there is a getTranslatedString() method, which returns the translation, and translate() always to return an object? As written in a book: never return nulls, or you are most likely to get a null pointer exception somewhere ;) Yeah, it does not return null, but returning not an object from a method that return an object as well, is the same problem anyway.

Once again, thank you for taking your time to explain this to me.

Re: Why Zend_View_Helper_Translate->translate() returns $this

Posted: Sat Feb 27, 2010 7:54 am
by Eran
You could file an issue with this at their issue tracker. They're usually pretty responsive