Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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 continue to pass them through info $this->$name($arg, $arg, $arg ...) is there a right way to do this?

like image 252
tarnfeld Avatar asked Aug 11 '10 20:08

tarnfeld


3 Answers

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

like image 133
ircmaxell Avatar answered Nov 14 '22 01:11

ircmaxell


You can use this:

call_user_func_array("self::method", $parameters)
like image 43
Sebastian Hoitz Avatar answered Nov 14 '22 02:11

Sebastian Hoitz


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

like image 5
Benjamin Cremer Avatar answered Nov 14 '22 03:11

Benjamin Cremer