Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] Assigning to a reference returned by a function/method

Posted by Claude Pache 
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


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
I'd rather put everything that has to do with references in a pot and throw
it at the core of the sun
Sorry, only registered users may post in this forum.

Click here to login