Peter Danenberg pisze:
Quoth Justin Martin on Pungenday, the 30th of Discord:
If I recall correctly, you can use the 'use' keyword.

Thanks, Justin; that occurred to me, too. But the following results in
"Notice: Undefined variable: factorial":

  $factorial = function($n) use ($factorial) {
      if ($n == 1)
        return 1;
      else
        return $n * $factorial($n - 1);
    };

  print $factorial(3);

and eventually "PHP Fatal error:  Function name must be a string."

This is correct.

See: in the first example you were referencing previously defined $foo (which at the moment of first reference was NULL). It was only later replaces with closure itself but it worked because of the reference.

On the other hand when you copy variable you try to use undefined $factorial (thus the notice). It is because at the moment of "using" the variable it's undefined. Only later it becomes our closure, however then it's after evaluation of use.

Although this is probably more a generals topic. The lack of __FUNCTION__ inside the closure is however something i'd like to hear more about.

Any word on this from the devs?

TIA,
Marcin

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to