#32845 [Opn->Bgs]: Setting a static variable by reference makes the variable non-static
| From: | sniper@php.net | Date: | Wed, 27 Apr 2005 10:48:28 +0000 |
| Subject: | #32845 [Opn->Bgs]: Setting a static variable by reference makes the variable non-static | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-78272@lists.php.net to get a copy of this message | ||
ID: 32845
Updated by: sniper@php.net
Reported By: justinpatrin@php.net
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Linux
PHP Version: 4.3.10, 5.0.4
New Comment:
RTFM:
http://www.php.net/manual/en/language.variables.scope.php
And the part with title:
"References with global and static variables"
Previous Comments:
------------------------------------------------------------------------
[2005-04-26 19:40:08] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=32845&edit=1