开发者

PHP How to call parent::__call() and pass in the arguments

开发者 https://www.devze.com 2023-01-11 08:35 出处:网络
If I am overloading the __call method in a PHP class, how could I call the actual method if my code doesn\'t do something else? For example:

If I am overloading the __call method in a PHP class, how could I call the actual method if my code doesn't do something else? For example:

public function __call($name, $arguments)
{
    if($name == 'tom')
    {
        $this->doName($name);
    }
    else
    {
        // Something here to carry on the __call maybe:
        // $this->$name($arguments);
    }
}

The issue is that the $arguments are passed through as an array, how could I c开发者_开发百科ontinue to pass them through info $this->$name($arg, $arg, $arg ...) is there a right way to do this?


You can call:

return parent::__call($name, $arguments);

Or am I misunderstanding what you're asking?

Edit:

And if you mean you want to call $this->$name() to try to trigger parent::__call(), it won't work. You'll wind up in an infinite loop until the stack overflows and php crashes (in other words, it's bad)...


You can use this:

call_user_func_array("self::method", $parameters)


http://php.net/manual/en/function.call-user-func-array.php is what you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消