Issues

ZF-11683: Zend_View ignore callable properties

Description

I have a class, for example:

class App_SomeClass
{
    public method someMethod()
    {
        // ...
    }
    public method __invoke()
    {
        // ...
    }
}

Pass the link to view: $this->view->someObject = new App_SomeClass(); Now we can call someMethod(): $this->someObject->someMethod(); {color:red}But we can not do so: $this->someObject(); Because Zend begins to look the same name Helpers, Plugins, and so on, ignoring the property.{color} I propose a solution:

abstract class Zend_View_Abstract implements Zend_View_Interface
{
    // ...

    public function __call($name, $args)
    {
        // is callable property
        if (property_exists($this, $name)) {
            if (is_callable($this->$name)) {
                return call_user_func_array($this->$name, $args);
            }
        }

        // is the helper already loaded?
        $helper = $this->getHelper($name);

        // call the helper method
        return call_user_func_array(
            array($helper, $name),
            $args
        );
    }

    // ...
}

Thank you.

Comments

Actually, there's another issue at stake here: you cannot assign callables to properties and invoke them. For example, try the following:


class A
{
    public function __invoke()
    {
        echo __CLASS__;
    }
}

$o = new stdClass;
$o->a = new A;
$o->a();

You'll get a fatal error (call to undefined method).

So, the root cause is actually not how __call() operates, but how PHP operates.

Now, we could potentially add the functionality you indicate here. However, it smells like a BC break -- __call() has only ever proxied to helpers in the past, which has allowed having helpers and properties of the same name. If developers have properties that are callable currently, this would potentially break their systems, as instead of getting the exception, they may now get unexpected results.

In the meantime, while we decide this issue, for your usage, may I suggest using {{$this->someObject->__invoke()}}?

I agree that this request would be difficult to implement without causing some level of BC break. Should it be closed as "Wont' Fix" for ZF1, or is it still being considered for 1.12?

On thinking about this, I don't think we should do this in ZF1. I have some functionality in ZF2 to somewhat mimic this behavior, but it's definitely BC-breaking behavior against ZF1.

In ZF1, I'd do:


$helper = $this->someObject;
$helper();

This will work regardless of PHP version, as well as regardless of ZF version.