|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-08-27 06:58 UTC] tularis@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 24 04:00:01 2025 UTC |
Description: ------------ Hi I have problem in singleton object creation. I having Apache and PHP 5.2.5 Below is the singleton code. ========== CODE =============== POSTED HERE ========== CODE =============== Problem: i have opened a new browser and triggered this class, for the first time the result is fine, ie output was Intializing .... Now for the second time when i refreshed the page agiain it is displaying the same output, it is not entering the else part in the "static function getInstance() ". Also i have opened another browser at the same time , and again trigger this class , and also i am getting same dam.. output.. === Doubts============ 1) will PHP create a singleton object for per session or for entire engine (like singleton object in java for entire JVM). 2) if the singleton object is for entire engine , why it is getting initialized when i triggered the same class from some other browser,(ie) it should not echo "initializing ..." , it should echo "Already init ()". === Doubts============ Please describe me what is happening here. Regards waxtonmax Reproduce code: --------------- <?php class Logger { static private $singleton; private function __construct() { } private function __clone() { } static function getInstance() { echo " ENTERING FOR GET INSTANCE.."."<br>"; if(Logger::$singleton == null) { echo "Intializing ...."."<br>";; Logger::$singleton = new Logger(); } else { echo " Already init ()"."<br>";; } return Logger::$singleton; } /** * */ public function logWriter($serverity , $errMsg) { // DO SOME THING } } Logger::getInstance()->logWriter(1, "This is the file content" ); ?> Expected result: ---------------- Same instance should used across all the session. ie where new request come from different user, the same instance which is instantiated first should be used Actual result: -------------- But new instance is created for every new request for the same session .