How to call the current anonymous function in PHP?

Tower Source

I have an anonymous function which is supposed to call itself. However, I have no variable or function name at hand, so I was hoping to find a function that could do return "this" in context of functions. Is there such a thing?

Here's an example:

$f = function() use($bar, $foo) {
  // call this function again.
};

Calling like this:

call_user_func(__FUNCTION__);

Leads to this:

Warning: call_user_func() expects parameter 1 to be a valid callback, function '{closure}' not found or invalid function name

If I try to put $f in the use-list, then it says the variable is not defined (because it is not yet).

php

Answers

answered 6 years ago Nameless #1

http://www.php.net/manual/en/language.constants.predefined.php

Edit: Posted before code was given. Of course it doesn't work on anonymous functions.

call_user_func(__FUNCTION__, $param1, $param2);
call_user_func_array(__FUNCTION__, $params);

answered 6 years ago Tower #2

Okay, I found out the way to do this:

$f = function() use(&$f) {
  $f();
};

$f();

The key thing is to pass $f as a reference. Thus PHP does not try to pass a value but a reference to a memory slot.

answered 6 years ago Lylo #3

function i_dont_know() {
    call_user_func(__FUNCTION__,$params);
 //or
    $funcname = __FUNCTION__;
    $funcname($params);


}

answered 6 years ago Paul Dixon #4

__FUNCTION__ cannot be used in anonymous functions

Pass the variable holding the anonymous function as a reference in the 'use' clause....

$f = function() use($bar, $foo, &$f) {
   $f();
};

Tip of the hat to this answer.

answered 2 years ago TarranJones #5

I have an anonymous function which is supposed to call itself.

I prefer to use call_user_func_array(__FUNCTION__, $params); when calling a recursive function.

As your example doesn't have any arguments then i guess call_user_func(__FUNCTION__); would be better suited.

You would expect and hope the following code would work but that would be too easy.

$bar = 10;
$foo = 0;

$f = function() use (&$bar,$foo) {

    if($bar){ // condition needed to prevent infinite loop
        echo $bar-- . PHP_EOL;
        call_user_func(__FUNCTION__); // wont work
    }
};
$f();

The __FUNCTION__ "Magic constant" is unavailable to closures so the code needs to be adapted to allow the passing of the function variable. we can make the function available by passing it as a regular argument or via the use statement.

Function passed as argument

$bar = 10;
$foo = 0;

$f = function( $__FUNCTION__ = null ) use (&$bar, $foo) {

    if($__FUNCTION__ && $bar){
        echo $bar-- . PHP_EOL;
        call_user_func( $__FUNCTION__, $__FUNCTION__);
    }
};
$f ( $f );

Function passed via use statement

$bar = 10;
$foo = 0;

$__FUNCTION__ = function() use (&$bar, $foo, &$__FUNCTION__) {

    if($bar){
        echo $bar-- . PHP_EOL;
        call_user_func( $__FUNCTION__ );
    }
};
$__FUNCTION__();

Working example, click edit-> ideone it! to re-run code.

comments powered by Disqus