Bug #80066 [Opn->Ver]: List Reference Assignment: Cannot reference an array value by return
| From: | requinix@php.net | Date: | Sun, 06 Sep 2020 20:53:17 +0000 |
| Subject: | Bug #80066 [Opn->Ver]: List Reference Assignment: Cannot reference an array value by return | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-228901@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80066&edit=1
ID: 80066
Updated by: requinix@php.net
Reported by: php-0919 at jan-runte dot de
Summary: List Reference Assignment: Cannot reference an array
value by return
-Status: Open
+Status: Verified
Type: Bug
Package: Variables related
Operating System: macOS 10.15.6
PHP Version: 7.3.22
Block user comment: N
Private report: N
New Comment:
PHP is complaining because the function doesn't return by-reference. If it does then
there's no error and everything works.
I don't think that requirement is necessary: the whole return value is not what is being
referenced, only the items inside, and array items can certainly be references without their parent
arrays being references too.
Previous Comments:
------------------------------------------------------------------------
[2020-09-06 19:40:24] php-0919 at jan-runte dot de
Description:
------------
Feature 7.3 - List Reference Assignment:
- https://wiki.php.net/rfc/list_reference_assignment
When attempting to transfer an array return value via the list reference assignment to certain keys
with by-reference, the error "Attempting to set reference to non referenceable value"
occurs.
But the belonging value of that key is a referenceable value - it is an array.
7.3.13: failed
7.3.22: failed
7.4.3: failed
7.4.10: failed
8.0.0beta2: failed
8.0.0beta3: failed
Test script:
---------------
function returnsArrayWithReferencedValue(array &$subject) : array {
return [
'a' => &$subject,
'b' => 123,
];
}
$source = [
'c' => [],
];
/*****************
/* PREFERRED WAY *
*****************/
['a' => &$referencedArray] = returnsArrayWithReferencedValue($source);
// Throws an error: "Attempting to set reference to non referenceable value in ..."
/**************
/* WORKAROUND *
**************/
// First set returned array to a variable
$returnedArray = returnsArrayWithReferencedValue($source);
$source['d'] = 456; // manipulate source array
var_dump($returnedArray['a']);
/**********
/* MOCKUP *
**********/
// Fake returned array as a prepared variable
$fakeReturnedArray = [
'a' => &$source,
'b' => 123,
];
['a' => &$referencedArray] = $fakeReturnedArray;
$source['d'] = 456; // manipulate source array
var_dump($referencedArray);
Expected result:
----------------
array(2) {
["c"]=>
array(0) {
}
["d"]=>
int(456)
}
Actual result:
--------------
Throws an error: Attempting to set reference to non referenceable value
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80066&edit=1