php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49717 Unable to recurse within an anonymous function
Submitted: 2009-09-29 22:06 UTC Modified: 2010-11-24 10:58 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: oliver dot saunders at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0 OS: Mac OS X
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: oliver dot saunders at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-29 22:08 UTC] 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.
 [2010-05-28 21:37 UTC] dchurch at sciencelogic dot com
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);
 [2010-11-24 10:58 UTC] jani@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: Scripting Engine problem
 [2010-11-24 10:58 UTC] jani@php.net
See last comment.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 12:01:33 2025 UTC