[英]PHP: access class method in callback function defined outside of class
我有一个像
class Foo
{
public function runWithCallback($custom_callback) {
...
return call_user_func_array($custom_callback, [$arg_a, $arg_b]);
}
public function aHelperMethod($arg_a) {
...
}
}
当我使用像
$foo = new Foo();
$foo->runWithCallback(function($arg_a, $arg_b) {
...
// now I need to use helper method "aHelperMethod"
$this->aHelperMethod($arg_c); // wrong code
...
});
当然,上面的代码不起作用,因为$this
在该匿名函数中没有任何意义。
可以按照我的意愿去做吗? 谢谢。
好的,感谢@BilalAhmed的评论,我发现这样做的“肮脏”方式:
$foo = new Foo();
$foo->runWithCallback(function($arg_a, $arg_b) use ($foo) {
...
// now I need to use helper method "aHelperMethod"
$foo->aHelperMethod($arg_c); // wrong code
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.