#21128 [Dup->WFx]: lost of static reference between calls
| From: | sniper@php.net | Date: | Sat, 18 Jan 2003 15:35:03 +0000 |
| Subject: | #21128 [Dup->WFx]: lost of static reference between calls | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-30471@lists.php.net to get a copy of this message | ||
ID: 21128
Updated by: sniper@php.net
Reported By: alex_boyer@hotmail.com
-Status: Duplicate
+Status: Wont fix
Bug Type: Scripting Engine problem
PHP Version: 4.2.3
New Comment:
..and see the reason in the above mentioned report..
Previous Comments:
------------------------------------------------------------------------
[2003-01-18 09:25:21] moriyoshi@php.net
Variant of bug #20175
------------------------------------------------------------------------
[2002-12-21 04:38:52] alex_boyer@hotmail.com
I want to keep a reference on a global object in a static method. But,
between calls of this static method, my reference becomes NULL !
Look at this simple self-explanatory code:
------------------------------------------------
class B {
var $x=100;
}
class A {
/*Returns an instance*/
function &getInstance(){
if( !isset($GLOBALS['foo']) ){
$GLOBALS['foo'] =& new B();
}
return $GLOBALS['foo'];
}
}
/*this function gets the instance and show information*/
function staticExecution(){
static $instance=NULL;
echo "ENTER staticExecution". "<BR/>\n";
//If static variable is not initialized
if( $instance === NULL ){
$instance =& A::getInstance();
echo "DEBUG: getInstance is called". "<BR/>\n";;
}
echo 'BEFORE INCREMENT:$instance->x=='.$instance->x."<BR/>\n";
$instance->x++;
echo 'AFTER INCREMENT: $instance->x == '.$instance->x .
"<BR/>\n";
echo "EXIT staticExecution". "<BR/>\n";;
}
//ten call to staticExecution
for ($i=0; $i<10; $i++){
staticExecution();
echo "<HR/>";
}
RESULTS:
------------------------------------------------
ENTER staticExecution
DEBUG: getInstance is called
BEFORE INCREMENT: $instance->x == 100
AFTER INCREMENT: $instance->x == 101
EXIT staticExecution
------------------------------------------------------------ENTER
staticExecution
DEBUG: getInstance is called
BEFORE INCREMENT: $instance->x == 101
AFTER INCREMENT: $instance->x == 102
EXIT staticExecution
------------------------------------------------------------ENTER
staticExecution
DEBUG: getInstance is called
BEFORE INCREMENT: $instance->x == 102
AFTER INCREMENT: $instance->x == 103
EXIT staticExecution
------------------------------------------------------------ENTER
staticExecution
DEBUG: getInstance is called
BEFORE INCREMENT: $instance->x == 103
AFTER INCREMENT: $instance->x == 104
EXIT staticExecution
------------------------------------------------------------
LIKE THIS TEN TIMES;
WHAT DO WE NOTICE:
- the reference returned is good: x is incremented
- BUT the debug message appears 10 times !!! Indeed, my static variable
is reinitialized between calls
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=21128&edit=1