> Am 01.09.2015 um 04:55 schrieb Rasmus Lerdorf <ras...@lerdorf.com>:
> 
>> On Aug 31, 2015, at 18:04, Bob Weinand <bobw...@hotmail.com> wrote:
>> 
>> From the RFC:
>>> all variables used in the body of the anonymous function will automatically 
>>> be bound to the anonymous function closure from the defining scope
>> 
>> The implementation is not capturing all the variables from the outer scope, 
>> but strictly only these accessed by the variables in the Closure itself. 
>> Hence that shouldn't be much of an issue.
> 
> This magic scares me. I made a very deliberate decision In the very first 
> implementation of PHP to avoid scope side-effects like this. Inside a 
> function everything is local unless explicitly declared not to be. This has 
> carried through for the past 20+ years in slightly different ways, but the 
> basic rule has stayed consistent even for closures. Unless explicitly 
> declared via a use clause, every variable inside a closure is local and no 
> cross-scope variable name side-effects are possible. I am not convinced that 
> this feature is worth breaking that longstanding basic tenet of the language.
> 
> -Rasmus

I'd like to note that it's binding by value and not by reference nor true 
closing over.
There are no side effects, only importing of the variables.

Also, as stated, an important application is quick closures to be passed to 
e.g. array_map:

/* increment by $y */
$y = 10;
$array = array_map($x ~> $x + $y, $array);

Here it is very obvious that we want to import a variable. Especially, I wonder 
how
$array = array_map(function ($x) use ($y) { return $x + $y; }, $array);
is making such simple applications more readable? And especially, what value 
does it add here?

If you want to be scoping-safe, (e.g. in bigger Closures inside a large 
function), there always is the explicit syntax allowing you to do that.
Short Closures are a tool for certain use cases, not a swiss knife.

Bob

P.s.: Just a side-question ... Why did you then think, it's be a great idea for 
include/require to share scope?
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to