Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow __call handle requests for view methods. #76

Closed
wants to merge 2 commits into from
Closed

Allow __call handle requests for view methods. #76

wants to merge 2 commits into from

Conversation

conormcd
Copy link
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
Copy link
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
Copy link
Contributor Author

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

@bobthecow
Copy link
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
Copy link
Contributor Author

Ah, I didn't think of that.

Thanks, and sorry for wasting your time.

@bobthecow
Copy link
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants