Why Zend_View_Helper_Translate->translate() returns $this

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Why Zend_View_Helper_Translate->translate() returns $this

Post 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);
    }
Last edited by Darhazer on Fri Mar 12, 2010 3:21 pm, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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');
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

You could file an issue with this at their issue tracker. They're usually pretty responsive
Post Reply