Is it possible to immediately evaluate an anonymous function?

Possible duplicate:
Immediately perform anonymous functions

I want to evaluate the anonymous function immediately, and not as a Closure object in the method arguments. Is it possible?

For instance:

$obj = MyClass;
$obj->Foo(function(){return "bar";}); // passes a Closure into Foo()
$obj->Foo(function(){return "bar";}()); // passes the string "bar" into Foo()?

The third line is the illegal syntax - is there a way to do this?

thank

+5
source share
1 answer

You can do this with call_user_func... although it can be a little silly if you can just assign it to a variable and subsequently call that variable.

call_user_func(function(){ echo "bar"; });

You might think that PHP 5.4 with its dereferencing capabilities will make this possible. However, you are mistaken (both on RC6, anyway).

+2
source

All Articles