在 PHP 中,我有一个接受函数对象并在线程中调用该函数的类:see the answer to this question .
它适用于匿名函数,但是,我想将此功能与不同类的静态函数一起使用:
$thThread = new FunctionThreader(OtherClass::static_function, $aParams);
这会引发错误Undefined class constant static_function on line x
我试过:
$fFunction = OtherClass::static_function;
$thThread = new FunctionThreader($fFunction, $aParams);
我得到了同样的错误。
那么有没有办法将这个静态函数存储到 $fFunction
中,或者简单地将它作为函数对象引用?
最佳答案
在 PHP 中,您通常会使用 callback为此:
$callback = array('ClassName', 'methodName');
$thThread = new FunctionThreader($callback, $aParams);
如果 FunctionThreader::__construct()
只接受一个 Closure
而你对其实现没有影响,那么你可以将静态函数调用包装在一个 关闭
:
$closure = function() { return ClassName::methodName(); };
$thThread = new FunctionThreader($closure, $aParams);
关于php - 将类的静态函数存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754099/