From:             pcdinh at gmail dot com
Operating system: Windows XP
PHP version:      5.1.4
PHP Bug Type:     Scripting Engine problem
Bug description:  Instantiate a singleton class returns many different objects

Description:
------------
Singleton designed class in PHP 5.1.4 does not really return a single
object after instantiated several times. 

My PC computer
PHP 5.1.4
Windows XP SP2
MySQL 5.0.20

Testing Server:  

http://pcdinh.dotgeek.org/singleton/info.php

Reproduce code:
---------------
I have created a singleton class like this:
[php]
class Singleton {

        private static $instance = null;

        private function __construct() {
        }

        public static function getInstance() {
                if (!isset (self :: $instance)) {
                        $className = __CLASS__;
                        self :: $instance = new $className;
                }
                return self :: $instance;
        }

}
[/php]

The test file 1 is as follows:

[php]
<?php
require_once './Singleton.php';

$singleton = Singleton :: getInstance();
$singleton2 = Singleton :: getInstance();

print "$singleton<br />";
print "$singleton2<br />";
?>
[/php]


The test file 2 is as follows:
[php]
<?php
require_once "Singleton.php";

// This line make problems
$e = new RuntimeException();

$singleton = Singleton :: getInstance();
$singleton2 = Singleton :: getInstance();

print "$singleton<br />";
print "$singleton2<br />";
?>
[/php]







Expected result:
----------------
I expected that test file 1 returns

Object id #1
Object id #1

and test file 2 returns

Object id #1
Object id #1

because the class is designed to return the same object every time. A
class based on the Singleton pattern properly instantiates and initializes
one instance of the
class and provides access to the exact same object every time. However, it
is not the case here.

Actual result:
--------------
Running the test file 1 returns:

Object id #1
Object id #1

That result is Ok. It implies that singleton designed class has worked
correctly.

Running the test file 2 right after that returns:

Object id #2
Object id #2

It means that in a same session, same computer and very short interval
time of script execution between 2 test file, I have got 2 different
results:

Object id #2
Object id #2

and 

Object id #1
Object id #1

So what is the different between 2 test files: I added one line to test
file 2: $e = new RuntimeException(); or $e = new Exception(). If I remove
them, I will get my expected results.

You can test it here:

http://pcdinh.dotgeek.org/singleton/

I think there is a bug in Zend Engine 2. I get confused with how singleton
classes work in a stateless environment as in PHP.


-- 
Edit bug report at http://bugs.php.net/?id=37437&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=37437&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=37437&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=37437&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=37437&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=37437&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=37437&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=37437&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=37437&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=37437&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=37437&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=37437&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=37437&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=37437&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=37437&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=37437&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=37437&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=37437&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=37437&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=37437&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=37437&r=mysqlcfg

Reply via email to