"Attempting to set reference to non referenceable value" upon array destructuring by reference
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
The following code:
<?php
$someglobal = 1;
function foo() {
global $someglobal;
return [&$someglobal];
}
list(&$ref) = foo();
++$ref;
var_dump($someglobal);
Resulted in this output:
Notice: Attempting to set reference to non referenceable value in /tmp/preview on line 10
int(1)
But I expected this output instead:
int(2)
Making foo return by reference silences this warning, i.e. this is a valid workaround:
function &foo() {
global $someglobal;
$ref = [&$someglobal];
return $ref;
}
But actually, the array itself should not need to return by reference, if we only desire to access a by-reference value within the array.
On top of this, this simple code:
<?php
[&$a] = [&$b];
emits Fatal error: Cannot assign reference to non referenceable value.
PHP Version
PHP 7.3-8.3
Operating System
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two destructuring examples on PHP 7.3–8.3 and compare their warnings, fatal error, and output with the reported expectations. Done means the first example increments the global to int(2) without the notice and the second no longer emits the fatal reference-assignment error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100