Is there any way to assign a static function to a variable, or accomplishing this in some other way?
class my_class{
public static function my_method($a){
return $a;
}
}
$some_func = my_class::my_method;
$someAnonFunc = function($a) use ($some_func){
return $some_func($a);
}
$inst = new SomeOtherClass(); //Defined somewhere else, in some other file
$inst->someMethod($a, $someAnonFunc);
At this point, I get:
Fatal error: Undefined class constant 'my_method'
Functions are not first class in PHP .. strings are. You would have to use call_user_func
if you wanted to stick with the ::
syntax as one unit:
$some_func = 'my_class::my_method';
$someAnonFunc = function ($a) use ($some_func) {
return call_user_func($some_func, $a);
}
Trying to run $some_func()
will not work since it will treat the colons as part of the function name.
Haven't tried this myself, but try
$some_func = 'my_class::my_method';
(e.g. assign the class/method as a string).
This may not work as is, but I do know that something like:
$x = 'mysql_real_escape_string';
$sql = "INSERT ... VALUES({$x($_GET['post'])})";
does. Ugly, but works.
Not sure if I understand your question correctly, but are you looking for call_user_func?
You can call the static method using
call_user_func(array('myclass', 'my_method'), $arg1, $arg2, ...);