假设我有一个如下数组:
array(
[0] => function() { return "hello"; }
[1] => function() { return "world"; }
[2] => "look"
[3] => function() { return "and";}
[4] => function() { return "listen";}
)
Run Code Online (Sandbox Code Playgroud)
有没有办法可以在不调用2的情况下调用0,1,3和4?
Thi*_*ter 16
匿名函数是Closure
类的实例.所以检查并is_callable
完成工作.
foreach ($array as $func) {
if (is_callable($func) && $func instanceof Closure) {
$func();
}
}
Run Code Online (Sandbox Code Playgroud)
实际上,类检查应该足够了,因为Closure
除非创建匿名函数,否则无法手动实例化对象.