【发布时间】:2017-02-11 15:25:07
【问题描述】:
看看这个示例代码:
我收到了错误:
致命错误:未捕获错误:不能在 C:\xampp\htdocs\dermaquality\test.php:11 中使用闭包类型的对象作为数组:11 堆栈跟踪:#0 C:\xampp\htdocs\dermaquality\test .php(20): test(Object(Closure)) #1 {main} 抛出
$array = array(
'hello' => function() {
echo "HEllo world";
}
);
function test( $func )
{
if (is_callable( $func['hello'] )) {
$func['hello']();
}
else {
$func();
}
}
echo "Executing 1 <br/>";
test( $hello = function() {"Hello world";} );
echo "Executing 2 <br/>";
test( $array['hello'] );
exit;
如果$func 是函数或$func['hello'] 是函数,我如何调用$func?
谢谢。
【问题讨论】:
-
尝试改变 if 做相反的事情: if (is_callable($func)) $func;否则 $func['hello']();
标签: php arrays function closures callable