Re: [RFC] Short Closures 2, aka auto-capture take 3

From: Date: Tue, 14 Jun 2022 15:45:23 +0000
Subject: Re: [RFC] Short Closures 2, aka auto-capture take 3
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-117945@lists.php.net to get a copy of this message
On 14/06/2022 12:18, Arnaud Le Blanc wrote:
- The closure mutates a value accessed through a captured variable. Mutable values include objects and resources, but NOT scalars or arrays (since they are copy-on-write).
It's not something that is used very often, so is often forgotten or ignored, but there is technically an edge case where arrays are mutable: they can contain references, and the references remain "live" even when the array itself is passed by value. So although unlikely, it is possible for a by-value closure to over-write variables in other scopes: https://3v4l.org/dPZlI // plain variable $a = 42; // array containing a reference $b = [
    'a' => &$a
]; // capture the array by-value $f = function() use($b) {
    // update the reference from inside the closure
    $b['a'] = 69;
}; // call it $f(); // observe that both the array and the plain variable now have the new value var_dump($a, $b); -- Rowan Tommins [IMSoP]

« previous php.internals (#117945) next »