php / php/php-src

"Attempting to set reference to non referenceable value" upon array destructuring by reference

Open
#13,046 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Category: Engine Status: Needs Triage
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.