ZF-7907: Current view helper strategy does not work with namespaced classes

Issue Type: Bug Created: 2009-09-21T05:50:53.000+0000 Last Updated: 2012-11-20T21:38:01.000+0000 Status: Open Fix version(s): Reporter: Giorgio Sironi (giorgiosironi) Assignee: None Tags: - Zend_View

Related issues: - ZF-7350



This class cannot be used as a view helper since the main method is treated as a constructor. The possibility of using a direct() method should be provided.

namespace NakedPhp\Mvc\View\Helper; use NakedPhp\Metadata\NakedObject; class DisplayObject extends \Zend_View_Helper_Abstract { public function __construct() { } public function displayObject() { return ''; } }


Posted by Giorgio Sironi (giorgiosironi) on 2009-09-21T05:57:39.000+0000

Workaround: use a __call method instead.

Posted by Raphael Dehousse (thymus) on 2010-07-15T00:58:46.000+0000


For this, I overload Zend_View_Abstract to add in the __call method

<pre class="highlight">
// call the helper method
if ( method_exists('direct', $helper) ) {
    $name = 'direct';
return call_user_func_array(
    array($helper, $name),

Posted by Raphael Dehousse (thymus) on 2010-07-15T01:53:00.000+0000

Error in my previous comment because Zend_View_Helper_Abstract has an empty "direct" method

<pre class="highlight">
if ( method_exists($helper, $name) ) {
    $methodName = $name;
} else {
    $methodName = 'direct';

return call_user_func_array(array($helper, $methodName), $args);

is working for me in 1.10.6 :)

Posted by Steven Rosato (ratius) on 2010-09-03T06:35:31.000+0000

As far as I am concerned, implementing the direct() function with required arguments will fail with a fatal error such as:

<pre class="highlight">
public function direct($key, $namespace = 'default',
        $returnModel = null, array $args = array())
    $modelContainer = \Zend_Controller_Front::getInstance()

     return $modelContainer->getModel($key, $namespace, $returnModel, $args);

<pre class="highlight">Fatal error: Declaration of Majisti\View\Helper\Model::direct() 
must be compatible with that of Zend_View_Helper_Interface::direct()

I have used Raph's overloading strategy, but with the function name 'helper' instead, though this would still forbid anyone to have a Helper named Helper, but that would be kind of unintuitive.

Posted by Matthew Weier O'Phinney (matthew) on 2010-09-03T06:50:41.000+0000

We've run into this in ZF2 already. The solution is that all arguments must be optional, and then you perform checks for required arguments as the first part of the body.

That said, we're likely moving to a slightly different paradigm for ZF2 anyways: you will retrieve your helper object from a broker, and simply call the methods you want:

<pre class="highlight">
echo $broker->load('display-object')->render($key, $namespace);

This is slightly more verbose, but also (a) faster (__call(), since it often uses call_user_func_array() internally, is around 6X slower than directly calling a method), (b) more explicit, and thus easier to understand (many newcomers do not understand where to find methods like "form()" and "headTitle()" -- the helper system is hard to grasp immediately), and (c) more portable (we can build the broker into other templating solutions easily).

I'm not sure if we can easily accommodate namespaced helpers in ZF1, to be honest.

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.