I want to call static method Status::compile() that has it's name stored in a variable like this $function = "Status::compile"; PHP: I tried $function(); PHP: but, as expected, it throws an error Fatal error: Call to undefined function Status::compile(). Is there any way to do it without using lambda functions (working in PHP 5.2.9) or eval()?
class Status { static function compile() { echo 'compile'; } } $string = "Status::compile"; list($class, $function) = explode('::', $string); call_user_func(array($class, $function)); PHP:
Excellent, works perfectly, even if i call it directly like call_user_func("Status::compile"); PHP: Thanks, premiumscripts.
I am assuming the OP is somehow building a static method name as a string and then would like to execute that method using the string value. The value may change and therefor could not be hard-coded as you have provided in your example.