Tom*_*Tom 5 php lambda anonymous-function
可能重复:
立即执行匿名函数
我想立即评估一个匿名函数,而不是它在方法args中作为Closure对象出现.这可能吗?
例如:
$obj = MyClass;
$obj->Foo(function(){return "bar";}); // passes a Closure into Foo()
$obj->Foo(function(){return "bar";}()); // passes the string "bar" into Foo()?
Run Code Online (Sandbox Code Playgroud)
第3行是非法语法 - 有没有办法做到这一点?
谢谢
小智 2
您可以使用call_user_func
... 来做到这一点,尽管当您可以将其分配给变量并随后调用该变量时,这可能有点愚蠢。
call_user_func(function(){ echo "bar"; });
您可能认为 PHP 5.4 及其取消引用功能将使这成为可能。然而,你错了(无论如何,从 RC6 开始)。