Dynamic class method invocation in PHP

Php

Php Problem Overview


Is there a way to dynamically invoke a method in the same class for PHP? I don't have the syntax right, but I'm looking to do something similar to this:

$this->{$methodName}($arg1, $arg2, $arg3);

Php Solutions


Solution 1 - Php

There is more than one way to do that:

$this->{$methodName}($arg1, $arg2, $arg3);
$this->$methodName($arg1, $arg2, $arg3);
call_user_func_array(array($this, $methodName), array($arg1, $arg2, $arg3));

You may even use the reflection api http://php.net/manual/en/class.reflection.php

Solution 2 - Php

You can use the Overloading in PHP: Overloading

class Test {

    private $name;

    public function __call($name, $arguments) {
        echo 'Method Name:' . $name . ' Arguments:' . implode(',', $arguments);
        //do a get
        if (preg_match('/^get_(.+)/', $name, $matches)) {
            $var_name = $matches[1];
            return $this->$var_name ? $this->$var_name : $arguments[0];
        }
        //do a set
        if (preg_match('/^set_(.+)/', $name, $matches)) {
            $var_name = $matches[1];
            $this->$var_name = $arguments[0];
        }
    }
}

$obj = new Test();
$obj->set_name('Any String'); //Echo:Method Name: set_name Arguments:Any String
echo $obj->get_name();//Echo:Method Name: get_name Arguments:
                      //return: Any String

[1]: http://php.net/manual/en/language.oop5.overloading.php "PHP Documentation"

Solution 3 - Php

Just omit the braces:

$this->$methodName($arg1, $arg2, $arg3);

Solution 4 - Php

You can also use call_user_func() and call_user_func_array()

Solution 5 - Php

If you're working within a class in PHP, then I would recommend using the overloaded __call function in PHP5. You can find the reference http://us.php.net/manual/en/language.oop5.overloading.php">here</a>;.

Basically __call does for dynamic functions what __set and __get do for variables in OO PHP5.

Solution 6 - Php

In my case.

$response = $client->{$this->requestFunc}($this->requestMsg);

Using PHP SOAP.

Solution 7 - Php

You can store a method in a single variable using a closure:

class test{        

    function echo_this($text){
        echo $text;
    }

    function get_method($method){
        $object = $this;
        return function() use($object, $method){
            $args = func_get_args();
            return call_user_func_array(array($object, $method), $args);           
        };
    }
}

$test = new test();
$echo = $test->get_method('echo_this');
$echo('Hello');  //Output is "Hello"

EDIT: I've edited the code and now it's compatible with PHP 5.3. Another example here

Solution 8 - Php

Still valid after all these years! Make sure you trim $methodName if it is user defined content. I could not get $this->$methodName to work until I noticed it had a leading space.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVirtuosiMediaView Question on Stackoverflow
Solution 1 - Phpandy.gurinView Answer on Stackoverflow
Solution 2 - PhpRodolfoNetoView Answer on Stackoverflow
Solution 3 - PhpKonrad RudolphView Answer on Stackoverflow
Solution 4 - PhpPeter BaileyView Answer on Stackoverflow
Solution 5 - PhpNoah GoodrichView Answer on Stackoverflow
Solution 6 - Phpuser46637View Answer on Stackoverflow
Solution 7 - PhpDavidView Answer on Stackoverflow
Solution 8 - PhpSnapeyView Answer on Stackoverflow