Search code examples
phprecursionreferenceanonymous-functionanonymous-recursion

Recursion with anonymous function


Possible Duplicates:
javascript: recursive anonymous function?
Anonymous recursive PHP functions

I was wondering... Is it possible to do recursion with anonymous function?

Here is one example: I need to get six-chars long string which may contain only numbers and spaces. The only rules are that it cannot start or end with spaces. We check for that and if that occurs - just call recursion on the same, anonymous, function. Just how!?

function() {

    $chars   = range(0, 9);
    $chars[] = ' ';
    length   = 6;
    $count   = count($chars);

    $string = '';
    for ($i = 0; $i < $length; ++$i) {

        $string .= $chars[mt_rand(0, $count - 1)];

    }

    $string = trim($string);

    if (strlen($string) !== $length) { // There were spaces in front or end of the string. Shit!

        // Do recursion.

    }

    return $string;

}

Solution

  • Yes it is, but I wouldn't recommend it as it's a bit tricky ;)

    First possibility:

    <?php
    $some_var1="1";
    $some_var2="2";
    function($param1, $param2) use ($some_var1, $some_var2)
    {
        call_user_func(__FUNCTION__, $other_param1, $other_param2);
    }
    ?>
    

    Another one:

    <?php 
    $recursive = function () use (&$recursive){ 
        // The function is now available as $recursive 
    } 
    ?> 
    

    Examples taken from http://php.net/