时间:2021-07-01 10:21:17 帮助过:45人阅读
0) {
return $test($a);
}
};
$test(10);正如上面的代码所示,我想在$test这个匿名函数里递归调用它自己,但是我发现在调用后会出现
Fatal error: Function name must be a string in /Library/WebServer/Documents/test.php on line 9
也就是$test这个变量并不能被识别,大家有办法能在匿名函数中递归函数本身吗?
php 5.2以后增加了匿名函数这个功能,但我在匿名函数递归时发现了问题
0) {
return $test($a);
}
};
$test(10);正如上面的代码所示,我想在$test这个匿名函数里递归调用它自己,但是我发现在调用后会出现
Fatal error: Function name must be a string in /Library/WebServer/Documents/test.php on line 9
也就是$test这个变量并不能被识别,大家有办法能在匿名函数中递归函数本身吗?
In order for it to work, you need to pass $test as a reference
$test = function ($a) use (&$test) {
...
}Reference: http://stackoverflow.com/questions/24...
tips: google helps.
php5.4下通过
$test = NULL;
$test = function ($a) use (&$test) {
echo $a;
$a --;
if ($a > 0) {
return $test($a);
}
};
$test(10);