#37437 [Com]: Instantiate a singleton class returns many different objects
ID: 37437
Comment by: daniel dot gorski at develnet dot org
Reported By: pcdinh at gmail dot com
Status: Open
Bug Type: Scripting Engine problem
Operating System: Windows XP
PHP Version: 5.1.4
New Comment:
>Running the test file 2 right after that returns:
>Object id #2
>Object id #2
Sure, because your $e is 'Object id#1'. Where is the problem? 'Object
id#2' == 'Object id#2', hence your singeton works.
If you create 123 other objects before your singleton intantiation, you
will get 'Object #124' as result.
regards dtg
Previous Comments:
------------------------------------------------------------------------
[2006-05-14 12:42:04] pcdinh at gmail dot com
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 this bug report at http://bugs.php.net/?id=37437&edit=1
Thread (3 messages)