我正在尝试编写一些嵌套的PHP匿名函数,结构就是你在下面看到的那个,我的问题是:如何使它无错误地工作?
$abc = function($code){
$function_A = function($code){
return $code;
};
$function_B = function($code){
global $function_A;
$text = $function_A($code);
return $text;
};
$function_B($code);
};
echo $abc('abc');
Run Code Online (Sandbox Code Playgroud)
输出是致命错误:函数名称必须是此行中的字符串:
$text = $function_A($code);
Run Code Online (Sandbox Code Playgroud)
这条消息对我没有说什么:(
tax*_*ala 10
这里的事情是你$function_A没有在全局范围内定义,而是在范围内定义$abc.如果您需要,可以尝试使用use,以便将您$function_A的范围传递到$function_B:
$abc = function($code){
$function_A = function($code){
return $code;
};
$function_B = function($code) use ($function_A){
$text = $function_A($code);
return $text;
};
$function_B($code);
};
Run Code Online (Sandbox Code Playgroud)