In JavaScript, you can define anonymous functions that are executed immediately:
(function () { /* do something */ })()
Can you do something like that in PHP?
Join Stack Overflow to learn, share knowledge, and build your career.
In JavaScript, you can define anonymous functions that are executed immediately:
(function () { /* do something */ })()
Can you do something like that in PHP?
For PHP7: see Yasuo Ohgaki's answer: (function() {echo 'Hi';})();
For previous versions: the only way to execute them immediately I can think of is
call_user_func(function() { echo 'executed'; });
In PHP 7
is to do the same in javascript
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val) {
echo $val, PHP_EOL;
}
echo $gen->getReturn(), PHP_EOL;
The output is:
1
2
3
Well of course you can use call_user_func
, but there's still another pretty simple alternative:
<?php
// we simply need to write a simple function called run:
function run($f){
$f();
}
// and then we can use it like this:
run(function(){
echo "do something";
});
?>
run
on your function in some other part of the code because there is no handle to your function existing after the line which immediately executes it.
– Pacerier
Sep 18 '13 at 13:31
run()
is to immediately execute the unnamed function passed to it. Same as call_user_func()
, only no parameters are passed.
– Cypher
Feb 7 '14 at 23:02
$f();
) and might be faster if the engine you use does not optimize for the special case where call_user_func
has only one function argument. This is because call_user_func
supports passing multiple parameters and it's first argument supports either a string as an argument or a function. That said, if call_user_func
is much readable, I'd not use run
unless the code is located somewhere at the bottom of the pyramind.
– Pacerier
Sep 2 '15 at 8:30
run
and call_user_func
. call_user_func
has an inherent disadvantage when compared to run
because run
does only one thing, whereas call_user_func
supports additional features in addition to doing what run
does. You can try a quick loop test (e.g.) to see which is faster on your engine.
– Pacerier
Sep 3 '15 at 4:33
This is the simplest for PHP 7.0 or later.
php -r '(function() {echo 'Hi';})();'
It means create closure, then call it as function by following "()". Works just like JS thanks to uniform variable evaluation order.
(new ReflectionFunction(function() {
// body function
}))->invoke();
Note, accepted answer is fine but it takes 1.41x as long (41% slower) than declaring a function and calling it in two lines.
[I know it's not really a new answer but I felt it was valuable to add this somewhere for visitors.]
Details:
<?php
# Tags: benchmark, call_user_func, anonymous function
require_once("Benchmark.php");
bench(array(
'test1_anonfunc_call' => function(){
$f = function(){
$x = 123;
};
$f();
},
'test2_anonfunc_call_user_func' => function(){
call_user_func(
function(){
$x = 123;
}
);
}
), 10000);
?>
Results:
$ php test8.php
test1_anonfunc_call took 0.0081379413604736s (1228812.0001172/s)
test2_anonfunc_call_user_func took 0.011472940444946s (871616.13432805/s)
I tried it out this way, but it's more verbose than the top answer by using any operator (or function) that allows you to define the function first:
$value = $hack == ($hack = function(){
// just a hack way of executing an anonymous function
return array(0, 1, 2, 3);
}) ? $hack() : $hack();
$hack = function(){...}; $hack()
?
– user5147563
Jul 8 '17 at 18:39
This isn't a direct answer, but a workaround. Using PHP >= 7. Defining an anonymous class with a named method and constructing the class and calling the method right away.
$var = (new class() { // Anonymous class
function cool() { // Named method
return 'neato';
}
})->cool(); // Instantiate the anonymous class and call the named method
echo $var; // Echos neato to console.
Not executed inmediately, but close to ;)
<?php
$var = (function(){ echo 'do something'; });
$var();
?>
call_user_func(function(){/* stuff */})
do? – Jasper Aug 25 '10 at 20:19