#27724 [Com]: static variables do not behave like expected

From: Date: Sun, 28 Mar 2004 07:05:54 +0000
Subject: #27724 [Com]: static variables do not behave like expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-56969@lists.php.net to get a copy of this message
 ID:               27724
 Comment by:       jewfish at jewfish dot net
 Reported By:      leonel at nied dot unicamp dot br
 Status:           Open
 Bug Type:         Class/Object related
 Operating System: Fedora Core1
 PHP Version:      5.0.0RC1
 New Comment:

I have seen this bug in PHP 4 as well (encountered it the same day you
posted this bug, in fact).  It can be verified to be in PHP 4 in much
the same way.


Previous Comments:
------------------------------------------------------------------------

[2004-03-27 11:50:55] kase at gmx dot net

Works well using this code:

kasedebian:/var/www# cat test.php5
<?php
class IntegerFactory {
        private static $int = NULL;

        function __construct() {}

        public function getInstance() {
                if ( !isset(IntegerFactory::$int) ) {
                        echo 'null !', "\n"; IntegerFactory::$int = new
IntegerFactory;
                }
                return IntegerFactory::$int;
        }
}

$tmp1 = new IntegerFactory;
$d1 = $tmp1->getInstance();
var_dump($d1);
$tmp2 = new IntegerFactory;
$d2 = $tmp2->getInstance();
var_dump($d2);
$d3 = $tmp1->getInstance();
var_dump($d3);
?>

------------------------------------------------------------------------

[2004-03-26 15:21:46] leonel at nied dot unicamp dot br

Funny enough, I remember now that I got this code from an article from
Andy Gutsman himself:

http://www.zend.com/php5/andi-book-excerpt.php
(see 10.static members)

------------------------------------------------------------------------

[2004-03-26 15:20:13] leonel at nied dot unicamp dot br

Description:
------------
I tried to implement a class with the Singleton pattern. However,
static variables do not seem to tie to the class, but rather to the
instance of the class.

Looking at bug 16245
  http://bugs.php.net/bug.php?id=16245&edit=2
I see that Mr. Flying and I have sort of 'exchanged' bugs. He got the
result I expected and I got the result he expected.
Notice 2 things: first, he assigned a primitive type to his variable,
whereas my variable holds an object; second, his variable is declared
inside the function, and mine is declared inside the class.

Reproduce code:
---------------
<?php
class IntegerFactory {
    private static $int = NULL;
    function __construct() {}
    public function getInstance() {
        if ( !isset($this->int) ) {
            echo 'null !', "\n"; $this->int = new IntegerFactory;
        }
        return $this->int;
    }
}
$tmp1 = new IntegerFactory;
$d1 = $tmp1->getInstance();
var_dump($d1);
$tmp2 = new IntegerFactory;
$d2 = $tmp2->getInstance();
var_dump($d2);
$d3 = $tmp1->getInstance();
var_dump($d3);
?>

Expected result:
----------------
null !
object(IntegerFactory)#2 (0) {
}
object(IntegerFactory)#2 (0) {
}
object(IntegerFactory)#2 (0) {
}


Actual result:
--------------
null !
object(IntegerFactory)#2 (0) {
}
null !
object(IntegerFactory)#4 (0) {
}
object(IntegerFactory)#2 (0) {
}



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=27724&edit=1


Thread (6 messages)

« previous php.bugs (#56969) next »