From:             [EMAIL PROTECTED]
Operating system: 
PHP version:      4.2.3
PHP Bug Type:     Scripting Engine problem
Bug description:  lost of static reference between calls

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 bug report at http://bugs.php.net/?id=21128&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=21128&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=21128&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=21128&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=21128&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=21128&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=21128&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=21128&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=21128&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=21128&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=21128&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=21128&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=21128&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=21128&r=isapi

Reply via email to