Re: Lexical scoping

From: Date: Tue, 28 Jan 2014 13:25:23 +0000
Subject: Re: Lexical scoping
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
2014-01-28 Sebastian Krebs <[email protected]>

> 2014-01-28 Andrea Faulds <[email protected]>
>
> > Good morning,
> >
> > Perhaps a future version of PHP (PHP 6? 5.7?) could have lexical scoping
> > with a
> > "let" keyword? At the very least, it would make foreach() with a
> reference
> > less
> > of a problem:
> >
> >     foreach ($array as let &$a) {
> >         $a = 3;
> >     }
> >     // $a is not in this scope - no need to worry about accidentally
> > changing
> > last array item
> >
> > Any thoughts?
> >
>
> Hi,
>
> My 2 cents: That is a very minor improvement at the cost of an additional
> keyword. It is only to avoid, that you accidentally interfere with outers
> scope variables? If it is hard to track these variables, maybe your
> method/function is too big.
>
>
I agree.


> Regarding your example:
>
> foreach ($array as &$a) {
>     $a = 3;
> }
> $a = null; // $a not a reference anymore. Problem solved :)
>
>
Problem not solved. You have just changed the last element of $array...
This is the exact problem that was to be avoided.


Lazare INEPOLOGLOU
Ingénieur Logiciel



> Regards,
> Sebastian
>
>
> > --
> > Andrea Faulds
> > http://ajf.me/
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
> --
> github.com/KingCrunch
>


Thread (11 messages)

« previous php.internals (#71668) next »