ZF-9393: Zend_Dojo_View_Helper_Dojo should have String representation method "toString"

Description

View helpers like headMeta, headTitle, headLink, etc have a toString method string representation method, the dojo helper doesn't, it should.

e.g.

echo $this->getHelper('headMeta'); echo $this->getHelper('headMeta')->toString();

will both work.

echo $this->getHelper('dojo');

doesn't, it fails "Catchable fatal error: Object of class Zend_Dojo_View_Helper_Dojo could not be converted to string"

And

echo $this->getHelper('dojo')->toString();

won't work because there is no toString method "Zend_Dojo_View_Exception: Invalid method "toString" called on dojo view helper"

Comments

Why are you using getHelper()?

The intended use of the Dojo view helper is using Zend_View as a proxy:


echo $this->dojo();

This ensures that the appropriate method (Zend_Dojo_View_Helper_Dojo::dojo()) is called, and that the return value from it is used. If you look at that method, it actually returns an instance of Zend_Dojo_View_Helper_Dojo_Container, which contains toString() and __toString(), and which can be echoed both directly or by calling the toString() method.

(The reason for this design is that the container object is placed in a registry, which ensures that it exists across multiple instances of Zend_View -- particularly useful when using partials.)

Zend_Dojo_View_Helper_Dojo_Container contains a "__toString" method but it does NOT contain a "toString" method, ZF 1.10.2.

echo $this->getHelper('dojo')->toString() is more instructive and concrete about what is actually happening than echo $this->dojo().

  • though I'm sure it might be trivial it's one less "call_user_func_array" call & 1 less call to a method of the helper itself (i.e. the helper named method, e.g. in headMeta it's headMeta, (needless if you're not passing in any parameters).

If getHelper is not intended to be used this way it should be protected or not exist at all, I don't see any other use-case for it.

"This ensures that the appropriate method (Zend_Dojo_View_Helper_Dojo::dojo()) is called, and that the return value from it is used ... (The reason for this design is that the container object is placed in a registry, which ensures that it exists across multiple instances of Zend_View - particularly useful when using partials.)"

View heleprs headMeta, headTitle, headLinks, etc are no different yet $this->getHelper('headMeta')->toString() works.

that was:

echo $this->getHelper('dojo')>toString()

is more instructive and concrete about what is actually happening than

echo $this>dojo().

That wasn't supposed to be crossed out, but I'm not able to edit that comment.

getHelper() is primarily intended for retrieving the helper in order to inject dependencies; in most cases, you utilize helpers by calling them as methods of the view object.

I'm re-opening this as a feature request.