# Lexical scoping

- Thread: https://externals.io/message/71663
- Original archive: https://news-web.php.net/php.internals/71663
- Started by Andrea Faulds on 2014-01-28T10:01:21+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #71663 — Andrea Faulds — 2014-01-28T10:01:21+00:00

URL: https://externals.io/message/71663#71663

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?
--
Andrea Faulds
http://ajf.me/

---

## #71667 — Sebastian Krebs — 2014-01-28T12:31:30+00:00

URL: https://externals.io/message/71663#71667 · In reply to: #71663

2014-01-28 Andrea Faulds <ajf@ajf.me>

> 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.

Regarding your example:

foreach ($array as &$a) {
    $a = 3;
}
$a = null; // $a not a reference anymore. Problem solved :)

Regards,
Sebastian


> --
> Andrea Faulds
> http://ajf.me/
>
> --
> 
> 
>
>


-- 
github.com/KingCrunch

---

## #71668 — Lazare Inepologlou — 2014-01-28T13:25:23+00:00

URL: https://externals.io/message/71663#71668 · In reply to: #71667

2014-01-28 Sebastian Krebs <krebs.seb@gmail.com>

> 2014-01-28 Andrea Faulds <ajf@ajf.me>
>
> > […]
> reference
> > […]
>
> 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
>
>
> > […]
>
>
> --
> github.com/KingCrunch

---

## #71669 — Christian Stoller — 2014-01-28T13:33:42+00:00

URL: https://externals.io/message/71663#71669 · In reply to: #71668

> > […]
> Problem not solved. You have just changed the last element of $array...
> This is the exact problem that was to be avoided.
>

`unset($a)` after the foreach solves the problem.

---

## #71670 — George Bond — 2014-01-28T13:36:34+00:00

URL: https://externals.io/message/71663#71670 · In reply to: #71668

> 2014-01-28 Sebastian Krebs <krebs.seb@gmail.com>
>
> > […]
> scoping
> > […]
> I agree.
>
>
> > […]
> Problem not solved. You have just changed the last element of $array...
> This is the exact problem that was to be avoided.
>
>
AFAIK, this is a problem which is quite limited to foreach() (are there
other examples?), so one (BC-breaking-but-probably-for-the-best) solution
would simply be to unset the variable automatically if it wasn't already
defined:

foreach( $array as &$a ){
    $a = 3;
}
var_dump( $a ); // E_NOTICE, $a is now undefined

$b = 2;
foreach( $array as &$b ){
    $b = 3;
}
var_dump( $b ); // 3, $b was defined before we started the foreach, so it's
not wiped

Alternatively, you could say that the ultimate problem is that we don't
have strong scoping - braces only define flow control, not scope.  So you
could introduce a strong-scoping structure.  Maybe a double brace?

foreach( $array as &$a ){{
    $a = 3;
}}
var_dump( $a ); // E_NOTICE, $a is now undefined

You could even say that an isolated brace pair (ie not one following a
control statement) *always* produces a scope.

--GB

---

## #71671 — Sebastian Krebs — 2014-01-28T14:05:22+00:00

URL: https://externals.io/message/71663#71671 · In reply to: #71668

2014-01-28 Lazare Inepologlou <linepogl@gmail.com>

>
> 2014-01-28 Sebastian Krebs <krebs.seb@gmail.com>
>
>  2014-01-28 Andrea Faulds <ajf@ajf.me>
> > […]
> I agree.
>
>
> > […]
> Problem not solved. You have just changed the last element of $array...
> This is the exact problem that was to be avoided.
>

Touche ;)
then use unset(). This time I tried it before:
http://codepad.viper-7.com/lOx1XL

>
>
> Lazare INEPOLOGLOU
> Ingénieur Logiciel
>
>
>
> > […]
>
>


-- 
github.com/KingCrunch

---

## #71675 — Andrea Faulds — 2014-01-28T16:06:01+00:00

URL: https://externals.io/message/71663#71675 · In reply to: #71667

> foreach ($array as &$a) {
>      $a = 3;
> }
> $a = null; // $a not a reference anymore. Problem solved :)

You just changed the last element of the array to null.

See why I proposed this? ;)
-- 
Andrea Faulds
http://ajf.me/

---

## #71676 — Andrea Faulds — 2014-01-28T16:07:57+00:00

URL: https://externals.io/message/71663#71676 · In reply to: #71670

> Alternatively, you could say that the ultimate problem is that we don't
> have strong scoping - braces only define flow control, not scope.  So you
> could introduce a strong-scoping structure.  Maybe a double brace?
>
> foreach( $array as &$a ){{
>      $a = 3;
> }}
> var_dump( $a ); // E_NOTICE, $a is now undefined
>
> You could even say that an isolated brace pair (ie not one following a
> control statement) *always* produces a scope.

My proposal is to add optional strong scoping. As JavaScript has var for 
function-scoped variables and ECMAScript Harmony will add let for 
lexically-scoped variables, I'm proposing something similar here. It 
would allow you to scope to the block, if you so desire.

-- 
Andrea Faulds
http://ajf.me/

---

## #71686 — Andrea Faulds — 2014-01-28T17:11:48+00:00

URL: https://externals.io/message/71663#71686

> Figure out how that looks to the runtime and we can start a
> conversation.  Until then, you're asking for something that PHP's
> engine is ill-equipped to introduce.

Is that really the case? If we don't permit variable shadowing here 
(i.e. you cannot do "$var = x; { let $var = y; } var_dump($var);"), then 
it would be as simple internally as unsetting the variable at the end of 
the block.
-- 
Andrea Faulds
http://ajf.me/

---

## #71716 — Stas Malyshev — 2014-01-29T08:40:41+00:00

URL: https://externals.io/message/71663#71716 · In reply to: #71663

Hi!

> 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

Typing unset($a) takes about 3 secs. Since you need to make additions
anyway, and you can already easily do it in the language - I don't see
any need for introducing new syntax. The use case for it seems pretty
small and already served by unset().
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #71722 — Sebastian Krebs — 2014-01-29T11:11:03+00:00

URL: https://externals.io/message/71663#71722 · In reply to: #71675

2014-01-28 Andrea Faulds <ajf@ajf.me>

>
>
> 
>
> > […]
>
> You just changed the last element of the array to null.
>
> See why I proposed this? ;)


I already fixed this... Read the other messages as well:
http://codepad.viper-7.com/lOx1XL


>
> --
> Andrea Faulds
> http://ajf.me/
>



-- 
github.com/KingCrunch
