Re: Re: Unwrap reference after foreach
| From: | Rowan Tommins | Date: | Sun, 14 Nov 2021 21:26:34 +0000 |
| Subject: | Re: Re: Unwrap reference after foreach | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-116352@lists.php.net to get a copy of this message | ||
On 14/11/2021 19:49, Côme Chilliet wrote:
Le 14 novembre 2021 18:13:18 GMT+01:00, Nikita Popov <nikita.ppv@gmail.com> a écrit :I think I agree with both of you - I agree that this is a problem worth solving, but also that the proposed solution isn't quite the right one. For one thing, it's just too "magic": although the current behaviour is *surprising*, it's actually a consequence of very straight-forward language rules; the proposed solution introduced a special case that isn't particular easy to explain, with extra edge cases to watch out for. One alternative solution would be to introduce block-scoped variables, which most of the time is what people want (and expect) in *any* foreach loop. Then the special-case for by-ref unsetting goes away, e.g.: foreach ( $array as let $x ) {I have ultimately decided to withdraw this proposal.This is sad to hear, because this is a really big footgun, which has hit me in the past. I ended up adding a codestyle rule forcing byref foreach to be followed by an unset to avoid this. I understand making a special case for one syntax and not the other is unsatisfying but I was hoping for an other outcome.
var_dump($x);} var_dump($x); //null / undefined foreach ( $array as let &$x ) { // or "&let $x"?
$x++;} var_dump($x); //null / undefined, not a reference Regards, -- Rowan Tommins [IMSoP]