php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45930 Problem in Singletion object creation
Submitted: 2008-08-27 06:18 UTC Modified: 2008-08-27 06:58 UTC
From: waxtonmax at yahoo dot com Assigned:
Status: Not a bug Package: Session related
PHP Version: 5.2.6 OS: windows vista
Private report: No CVE-ID: None
 [2008-08-27 06:18 UTC] waxtonmax at yahoo dot com
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 .

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-27 06:58 UTC] tularis@php.net
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.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 18:01:31 2025 UTC