Php 尝试将带有参数的回调函数作为函数参数传递-获取;意外的callbackFunctionName,应为';(“x27”错误
我正在尝试使用并调用setInterval()函数。从链接的github页面:Php 尝试将带有参数的回调函数作为函数参数传递-获取;意外的callbackFunctionName,应为';(“x27”错误,php,callback,anonymous-function,declare,callable-object,Php,Callback,Anonymous Function,Declare,Callable Object,我正在尝试使用并调用setInterval()函数。从链接的github页面: /** * Just for simplifying the Timers::setInterval method * * * @param callable | string $func * @param float $milliseconds * * @return integer */ function setInterval ($func, $milliseconds) { retur
/**
* Just for simplifying the Timers::setInterval method
*
*
* @param callable | string $func
* @param float $milliseconds
*
* @return integer
*/
function setInterval ($func, $milliseconds)
{
return Timers::setInterval($func, $milliseconds);
}
如您所见,它将函数作为第一个参数,因此我尝试向它传递一个回调函数
declare(ticks=1) {
setInterval(function callbackFunction() use $someArrayFromOuterScope {
runSomeOtherFunction();
//Do something
}, $someArrayFromOuterScope[0]["time"]);
}
但我得到了一个错误:
分析错误:语法错误,意外调用函数,应为“(”
所以问题是我做错了什么,我该如何纠正它?试试这个
function setInterval ($func, $milliseconds)
{
return Timers::setInterval($func, $milliseconds);
}
declare(ticks=1) {
setInterval(function($someArrayFromOuterScope) {
runSomeOtherFunction();
//Do something
}, $someArrayFromOuterScope[0]["time"]);
}
任何匿名函数都不应该有名称。这是您在此处的语法错误。“试试这个”并不是一个好的答案。您应该解释这是如何以及为什么解决了它们的问题。我建议您阅读,“