php.net |  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: justinpatrin@php.net 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
 [2005-04-26 19:40 UTC] justinpatrin@php.net
Description:
------------
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;
or
$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/> ';
    }
}
a();
a();
function b() {
    static $w;
    $a = 0;
    if (!isset($w)) {
        $w = $a;
        echo 'setting w<br/> ';
    }
}
b();
b();

Expected result:
----------------
setting v
setting w

Actual result:
--------------
setting v
setting v
setting w

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-27 12:48 UTC] sniper@php.net
RTFM:   
  
  http://www.php.net/manual/en/language.variables.scope.php

And the part with title:

  "References with global and static variables"


 [2005-04-27 18:19 UTC] justinpatrin@php.net
Sorry about that, I didn't see that part of the manual. Still, this doesn't seem like good behavior...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 13 17:01:30 2024 UTC