Re: Re: Closures and __FUNCTION__

From: Date: Mon, 13 Apr 2009 18:10:29 +0000
Subject: Re: Re: Closures and __FUNCTION__
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-43678@lists.php.net to get a copy of this message
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

« previous php.internals (#43678) next »