Allow __call handle requests for view methods. #76

Closed
wants to merge 2 commits into
from

Projects

None yet

3 participants

@conormcd
Contributor

In the case where method_exists($view, $tag_name) fails and when isset($view->$tag_name) fails, this patch alters Mustache to attempt to call the method anyway. This allows the view to use __call to handle the method invocation.

Example:

class MyModel {
    public function baz() {
        return "baz";
    }
}
class MyView {
    public $foo = "foo";

    public function bar() {
        return "bar";
    }
}
class View {
    protected $_model;

    public function __construct($model) {
        $this->_model = $model;
    }

    public function __call($name, $args) {
        return call_user_func_array(array($this->_model, $name), $args);
    }
}

In this scenario, if we pass an instance of MyView to Mustache, then foo, bar and baz are all available.

@scribu
Contributor
scribu commented Jan 21, 2012

If the class doesn't have __call() defined and the method doesn't exist, it will result in a fatal error.

@conormcd
Contributor

Good catch @scribu ! I've updated the patch accordingly.

@bobthecow
Owner

Mustache.php cannot support __call magic methods. Please see #16, #19 and #55, and this gist.

@bobthecow bobthecow closed this Jan 21, 2012
@conormcd
Contributor

Ah, I didn't think of that.

Thanks, and sorry for wasting your time.

@bobthecow
Owner

No worries. It's obviously a common need. Unfortunately it just doesn't work right in PHP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment