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]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to