php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21128 lost of static reference between calls
Submitted: 2002-12-21 04:38 UTC Modified: 2003-01-18 09:35 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: alex_boyer at hotmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 4.2.3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: alex_boyer at hotmail dot com
New email:
PHP Version: OS:

 

 [2002-12-21 04:38 UTC] alex_boyer at hotmail dot 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-18 09:25 UTC] moriyoshi@php.net
Variant of bug #20175
 [2003-01-18 09:35 UTC] sniper@php.net
..and see the reason in the above mentioned report..

 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Jul 04 02:05:45 2022 UTC