ID: 37437 Updated by: [EMAIL PROTECTED] Reported By: pcdinh at gmail dot com -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: Windows XP PHP Version: 5.1.4 New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a list of more appropriate places to ask for help using PHP, please visit http://www.php.net/support.php as this bug system is not the appropriate forum for asking support questions. Due to the volume of reports we can not explain in detail here why your report is not a bug. The support channels will be able to provide an explanation for you. Thank you for your interest in PHP. Previous Comments: ------------------------------------------------------------------------ [2006-05-14 12:56:54] daniel dot gorski at develnet dot org >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 ------------------------------------------------------------------------ [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
