Assigning to a reference returned by a function/method
| From: | Claude Pache | Date: | Fri, 31 Aug 2018 18:44:22 +0000 |
| Subject: | Assigning to a reference returned by a function/method | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-103108@lists.php.net to get a copy of this message | ||
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