查看此示例代码:
我收到了错误:
致命错误:未捕获错误:无法在C:\ xampp \ htdocs \ dermaquality \ test.php中使用Closure类型的对象: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
功能正常,我该如何致电$func['hello']
?
感谢。
答案 0 :(得分:1)
问题出现在if (is_callable( $func['hello'] )) {
,因为你不知道$func
是否是一个数组..顺便说一句,你不把数组作为参数放在test( $array['hello'] );
你只是放置函数......
function test( $func )
{
if (is_callable($func)) {
$func();
}
else if (is_array($func)){
if(isset($func['hello']) && is_callable($func['hello'])){
$func['hello']();
}else{
// unknown what to call
}
}else{
// unknown what to call
}
}