tags:

views:

974

answers:

5

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);
+1  A: 

Just omit the braces:

$this->$methodName($arg1, $arg2, $arg3);
Konrad Rudolph
Thanks. I had thought of that, but hadn't tried it yet.
VirtuosiMedia
+12  A: 

There is more then 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://it2.php.net/oop5.reflection

andy.gurin
I guess maybe I did have the syntax right, so something else is wrong with my code as it's not quite functioning correctly. Hmm...
VirtuosiMedia
+1  A: 

You can also use call_user_func() and call_user_func_array()

Peter Bailey
A: 

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

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

Noah Goodrich
A: 

In my case.

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

Using PHP SOAP.