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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 20:01:31 2024 UTC