> Am 22.09.2015 um 20:05 schrieb Dmitry Stogov <[email protected]>:
>
> The current PHP version emits two warning on similar constructs, and this
> is explainable because we explicitly "use" $y.
>
> $ sapi/cli/php -r 'function foo(){(function($x) use ($y){$y=3; return
> $y+$x;})(5);return $y;} var_dump(foo());'
> PHP Notice: Undefined variable: y in Command line code on line 1
>
> PHP Notice: Undefined variable: y in Command line code on line 1
>
> NULL
>
> Thanks. Dmitry.
Well, yes. But that obviously needs alternate handling here as we don't have
full analysis to look at whether we'd need to import or not. Hence we just are
importing everything if it's used in Closure scope and defined in parent scope.
Because we obviously don't want this to emit a notice:
function foo() {
return ($x ~> {
$y = 2;
return $x + $y;
})(10);
}
Else it'd be totally impossible to define any variables...
Also, the long form sets $y to NULL in the Closure (we just have two notices
there because you return $y; too… if you remove that, you'll be left with just
one notice.
Bob