php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27724 static variables do not behave like expected
Submitted: 2004-03-26 15:20 UTC Modified: 2004-04-21 00:14 UTC
Votes:4
Avg. Score:4.0 ± 1.7
Reproduced:3 of 4 (75.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: leonel at nied dot unicamp dot br Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.0.0RC1 OS: Fedora Core1
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: leonel at nied dot unicamp dot br
New email:
PHP Version: OS:

 

 [2004-03-26 15:20 UTC] 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) {
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-26 15:21 UTC] 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-27 11:50 UTC] 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-28 02:05 UTC] jewfish at jewfish dot net
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.
 [2004-04-13 13:06 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2004-04-21 00:14 UTC] iliaa@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 23:01:29 2024 UTC