Possible Duplicate:
Immediately executing anonymous functions
I want to immediately evaluate an anonymous function rather than it appearing as a Closure object in method args. Is this possible?
For example:
$obj = MyClass;
$obj->Foo(function(){return "bar";}); // passes a Closure into Foo()
$obj->Foo(function(){return "bar";}()); // passes the string "bar" into Foo()?
The 3rd line is illegal syntax -- is there any way to do this?
Thanks
You could do it with call_user_func
... though that might be a bit silly when you could just assign it to a variable and subsequently invoke the variable.
call_user_func(function(){ echo "bar"; });
You might think that PHP 5.4 with it's dereferencing capabilities would make this possible. You'd be wrong, however (as of RC6, anyway).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With