|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32845 Setting a static variable by reference makes the variable non-static
Submitted: 2005-04-26 19:40 UTC Modified: 2005-04-27 18:19 UTC
From: Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.10, 5.0.4 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
5 + 3 = ?
Subscribe to this entry?

 [2005-04-26 19:40 UTC]
When setting a static variable by reference (as one would want to do in PHP4 since the object model uses copies instead of references) the static variable becomes non-static and is lost when the function ends. I assume this could be fixed by making the reference itself static instead of the variable.

This can also be "fixed" by using a container as the static variable (an array or object) and setting the var by ref within the array/object.

$v[0] =& $var;
$v->v =& $var;

I also tested this in PHP 5.0.4 and the same thing happens. I realize that PHP5 uses refs instead of copies by default.

Reproduce code:
function a() {
    static $v;
    $a = 0;
    if (!isset($v)) {
        $v =& $a;
        echo 'setting v<br/> ';
function b() {
    static $w;
    $a = 0;
    if (!isset($w)) {
        $w = $a;
        echo 'setting w<br/> ';

Expected result:
setting v
setting w

Actual result:
setting v
setting v
setting w


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-27 12:48 UTC]

And the part with title:

  "References with global and static variables"

 [2005-04-27 18:19 UTC]
Sorry about that, I didn't see that part of the manual. Still, this doesn't seem like good behavior...
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Mar 26 10:04:44 2023 UTC