php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73169 Variable becames reference
Submitted: 2016-09-25 16:57 UTC Modified: 2016-10-03 20:51 UTC
From: kilobyte at freemail dot hu Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kilobyte at freemail dot hu
New email:
PHP Version: OS:

 

 [2016-09-25 16:57 UTC] kilobyte at freemail dot hu
Description:
------------
the code:

class CC
{
    private static $bt;

    public function run(&$a)
    {
        self::$bt[] = debug_backtrace();
        $a = 222;
    }
}

function n(&$a)
{
    (new CC())->run($a);
}

$a = [
    'a' => 1,
    'b' => 2,
    'c' => 3
];

n($a['b']);
echo '<pre>';
var_dump($a);

--------------------

this generates the following:

array(3) {
  ["a"]=>
  int(1)
  ["b"]=>
  &int(222)
  ["c"]=>
  int(3)
}

there is a "reference &" signal at "b". If I remove this line: "self::$bt[] = debug_backtrace();" its gone, it will work right, but why do this variable goes reference?


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-25 19:21 UTC] yohgaki@php.net
-Operating System: Windows 7 +Operating System: Irrelevant -PHP Version: 5.6Git-2016-09-25 (snap) +PHP Version: Irrelevant
 [2016-09-25 19:21 UTC] yohgaki@php.net
https://3v4l.org/ZLToT
All versions do the same. HHVM seems to have problem with this.
 [2016-10-03 20:51 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2016-10-03 20:51 UTC] bwoebi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

debug_backtrace() is storing the arguments of the stackframes as is, i.e. it retains references too.

Thus CC::$bt[0][0]["args"]["b"] will be another variable pointing to $a["b"].

If you remove this debug_backtrace() call, then, obviously, all the other variables pointing to $a["b"] have been destroyed and thus there's no reference anymore.
Using DEBUG_BACKTRACE_IGNORE_ARGS as first parameter to debug_backtrace() will have the same effect.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC