Assigning to a reference returned by a function/method

php.internals

Claude Pache

8 years ago
Hi internals, Today I tried something like that: <?php function &fooRef(...$args) { // ... impl details return $someRef; } fooRef('a', 'b') = 42; ?> and it failed miserably (Fatal Error: Can't use function return value in write context). Naturally, as workaround, I could write: <?php $ref =& fooRef('a', 'b'); $ref = 42; unset($ref); ?> which is passable for one instruction, but not so much for ten similar instructions in a row. Another hackier workaround: <?php function arrayFoo(...$args) { // ... impl details $arr = [ &$someRef ]; return $arr; } arrayFoo('a', 'b')[0] = 42; ?> (... and I was wondering that it worked, while <?php [ &fooRef('a', 'b') ][0] = 42; ?> triggered a fatal error). So... what do you think? Is it reasonable to allow to assign to a function/method return value, when it is a reference? —Claude

Marco Pivetta

8 years ago
I'd rather put everything that has to do with references in a pot and throw it at the core of the sun 🔥 Expanding features around references, especially since they are known to be problematic, makes them a bigger problem. On Fri, 31 Aug 2018, 20:44 Claude Pache, <claude.pache@gmail.com> wrote: