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


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


Workaround: use a __call method instead.


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

// call the helper method
if ( method_exists('direct', $helper) ) {
    $name = 'direct';
return call_user_func_array(
    array($helper, $name),

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

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 :)

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

public function direct($key, $namespace = 'default',
        $returnModel = null, array $args = array())
    $modelContainer = \Zend_Controller_Front::getInstance()

     return $modelContainer->getModel($key, $namespace, $returnModel, $args);
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.

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:

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.