php的匿名函数中如何递归自己
php 5.2以后增加了匿名函数这个功能,但我在匿名函数递归时发现了问题
<?php $test = NULL; $test = function ($a) use ($test) { echo $a; $a --; if ($a > 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
这个变量并不能被识别,大家有办法能在匿名函数中递归函数本身吗?
帅气的A姬
10 years, 8 months ago
Answers
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.
越不过的山巅
answered 10 years, 8 months ago