寻找将函数分配给类变量然后调用它的方法。
我知道您可以将类 var 分配给另一个变量,然后调用它,但我正在寻找一种无需创建临时变量和弄乱代码的方法。
临时变量或使用call_user_func()
是我唯一的选择吗?
class MyClass {
public $callMe;
public function __construct() {
$this->callMe = function(...$args) {
print_r(['HIT', $args]);
};
// $this->callMe('asd', 'sda');
// Error: PHP Fatal error: Call to undefined method test::callMe()
// This works, but I would prefer not to create temporary variables for this
$tmp = $this->callMe;
$tmp('sd','adsf','fdas');
}
}
尝试以下操作之一:
($this->callMe)('sd','adsf','fdas');
call_user_func($this-callMe, 'sd','adsf','fdas');