Re: [RFC] Explicit call-site pass-by-reference (again)

From: Date: Fri, 21 Feb 2020 02:29:18 +0000
Subject: Re: [RFC] Explicit call-site pass-by-reference (again)
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-108710@lists.php.net to get a copy of this message
This proposal is great, but most PHP static analysis tools already do a reasonable job of understanding by-reference assignment and detecting bugs there (an exception is closure use by-reference checks, which is a static-analysis no-man's land). No static analysis tools catch your specific use-case, though. On Thu, 20 Feb 2020 at 09:48, Levi Morrison via internals < internals@lists.php.net> wrote: > Just chiming in to voice strong support for this RFC. This is a key > piece toward making PHP code statically analyzable. If it becomes > required at the call site, such as in an edition of the language, it > will significantly enhance the ability to reason about code and > probably make it more correct as well. As a small example, consider > this method on an Optional type class: > > function map(callable $f): Optional { > if ($this->enabled) { > return new Optional($f($this->data)); > } else { > return $this; > } > } > > The intent is to return a new optional or an empty one, but if you > pass a closure that accepts something by reference you can change the > original, which is not intended at all. For people who defend against > it, it requires saving $this->data to a local variable, then > passing > in the local. Then if the user does a call-by-reference it will affect > the local, not the object's data. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.internals (#108710) next »