|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-09-29 22:06 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 16:00:01 2025 UTC |
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.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);