Re: Unwrap reference after foreach

From: Date: Thu, 26 Aug 2021 14:26:02 +0000
Subject: Re: Unwrap reference after foreach
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-115856@lists.php.net to get a copy of this message
On Sat, Aug 14, 2021 at 6:00 PM Claude Pache <claude.pache@gmail.com> wrote: > > Le 13 août 2021 à 15:28, Nikita Popov <nikita.ppv@gmail.com> a écrit : > > Hi internals, > > I'd like to address a common footgun when using foreach by reference: > https://wiki.php.net/rfc/foreach_unwrap_ref > > This addresses the issue described in the big red box at > https://www.php.net/manual/en/control-structures.foreach.php. > While this > is > "not a bug" (as our bug tracker can regularly attest), it's rather > unexpected, and we could easily avoid it... > > Regards, > Nikita > > > A case that should be mentioned in the RFC is destructuring: > > ```php > foreach ($foo as [ $x, &$y ]) { /* .... */ } > ``` > https://3v4l.org/A5Vi7 > > I assume that the reference in $y would be unwrapped after the > loop? > I clarified to the RFC to say that a) unwrapping also occurs if the loop is left using break, continue, goto etc. b) unwrapping occurs for array destructuring targets under the same limitation of "simple variables only". So yes, $y would be unwrapped in your example. Regards, Nikita

« previous php.internals (#115856) next »