javascript 如何在 PHP 中立即执行匿名函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3568410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I immediately execute an anonymous function in PHP?
提问by Emanuil Rusev
In JavaScript, you can define anonymous functions that are executed immediately:
在 JavaScript 中,您可以定义立即执行的匿名函数:
(function () { /* do something */ })()
Can you do something like that in PHP?
你能在 PHP 中做类似的事情吗?
回答by Gordon
For PHP7: see Yasuo Ohgaki's answer: (function() {echo 'Hi';})();
对于 PHP7:请参阅Yasuo Ohgaki 的回答:(function() {echo 'Hi';})();
For previous versions: the only way to execute them immediately I can think of is
对于以前的版本:我能想到的立即执行它们的唯一方法是
call_user_func(function() { echo 'executed'; });
回答by Wallace Maxters
In PHP 7is to do the same in javascript
在PHP 7javascript 中做同样的事情
$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
回答by Pacerier
Well of course you can use call_user_func, but there's still another pretty simple alternative:
当然你可以使用call_user_func,但还有另一个非常简单的选择:
<?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";
});
?>
回答by Yasuo Ohgaki
This is the simplest for PHP 7.0 or later.
这是最简单的 PHP 7.0 或更高版本。
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.
这意味着创建闭包,然后通过“()”将其作为函数调用。由于统一的变量评估顺序,就像 JS 一样工作。
回答by innermond
(new ReflectionFunction(function() {
// body function
}))->invoke();
回答by Shovas
Note, accepted answeris fine but it takes 1.41x as long (41% slower) than declaring a function and calling it in two lines.
请注意,接受的答案很好,但它比声明一个函数并在两行中调用它需要 1.41 倍的时间(慢 41%)。
[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)
回答by thecoolestname36
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.
这不是直接的答案,而是一种解决方法。使用 PHP >= 7. 使用命名方法定义匿名类并构造该类并立即调用该方法。
$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.
回答by Paul Jerome Bordallo
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();
回答by James
Not executed inmediately, but close to ;)
不是立即执行,而是接近 ;)
<?php
$var = (function(){ echo 'do something'; });
$var();
?>

