Page 1 of 1

accessing class method from variable

Posted: Fri Sep 12, 2008 11:51 am
by kendall
Hi,

How can I execute the method of a class that is passed via a variable?

e.g.

Code: Select all

 
// class
class M {
function send(){
}
}
 
$class_method = 'send'; // the method of class M
 
$test = new M();
 
thus the accessing of it will be

Code: Select all

 
$test->{$class_method}() 
 
or

Code: Select all

$test->{$class_method. '()'}
:?: :?: :?: :?: :?: :?: :?: :?:

Re: accessing class method from variable

Posted: Fri Sep 12, 2008 12:13 pm
by ahowell

Code: Select all

class foo
{
    
    public function bar()
    {
        echo 'bar';
    }
    
}
 
 
$foo = new foo();
 
$method = 'bar';
 
$foo->$method();