Edit report at http://bugs.php.net/bug.php?id=49717&edit=1
ID: 49717
Comment by: dchurch at sciencelogic dot com
Reported by: oliver dot saunders at gmail dot com
Summary: Unable to recurse within an anonymous function
Status: Open
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: Mac OS X
PHP Version: 5.3.0
New Comment:
This is the syntax you're looking for:
$c = function($n) use(&$c) {
if ($n > 0) {
echo $n . PHP_EOL;
$c($n - 1); } };
$c(4);
Previous Comments:
------------------------------------------------------------------------
[2009-09-29 22:08:10] oliver dot saunders at gmail dot com
You can workaround like this:
$c = function($self, $n) {
if ($n > 0) {
echo $n . PHP_EOL;
$self($self, $n - 1); } };
$c($c, 4);
...but that's not really the point.
------------------------------------------------------------------------
[2009-09-29 22:06:26] oliver dot saunders at gmail dot com
Description:
------------
Unable to recurse within an anonymous function.
Reproduce code:
---------------
$c = function($n) use($c) {
if ($n > 0) {
echo $n . PHP_EOL;
$c($n - 1); } };
$c(4);
Expected result:
----------------
4
3
2
1
Actual result:
--------------
4
Fatal error: Function name must be a string in Command line code on line
4
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=49717&edit=1