Bug #52653 [Opn->Csd]: multiple instances made with a singleton

From: Date: Fri, 20 Aug 2010 12:02:10 +0000
Subject: Bug #52653 [Opn->Csd]: multiple instances made with a singleton
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-153648@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52653&edit=1

 ID:                 52653
 User updated by:    paulscheltema at gmail dot com
 Reported by:        paulscheltema at gmail dot com
 Summary:            multiple instances made with a singleton
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            Reproducible crash
 Operating System:   debian
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

Ok great, thanks for the help.


Previous Comments:
------------------------------------------------------------------------
[2010-08-20 13:54:31] t-bader at gmx dot net

It's not a bug.

At test::getInstance() the __construct method is invoked which retrieves
the instance from test2. 

The __construct of test2 is invoked and clals test::getInstance(), since
the first call of test::getInstance doesn't finished the constructor
execution self::$instance will never be set and the __construct method
of test is invoked again.

Same with test2:getInstance, so you created an endless loop because the
__construct methods never can finish.

------------------------------------------------------------------------
[2010-08-20 11:28:36] paulscheltema at gmail dot com

Description:
------------
Hello, first off, ive been searching but i couldnt find any related
bugreport and im sorry if i wasted your time.

I have 2 classes with both an singleton function in them, an api (test)
and a user (test2) class. The api uses the user data and the user class
uses the api to get its userdata. Hence i load the user class instance
in the api and vice versa.

It works if i use an external class to serve as a singleton storage but
if i use the static $instance it doesnt. 
The singleton function does work when i remove the test2::getInstance()
from class test.

Test script:
---------------
class test {
	
	private static $instance;
	private $test2;
	
	private function __construct() {
		$this->test2 = test2::getInstance();
	}
	
	public static function getInstance() {
		if (!isset(self::$instance)) {
			echo '<br>new test instance';
            $c = __CLASS__;
            self::$instance = new $c;
        }
       return self::$instance;
	}

}

class test2 {

	private static $instance;
	private $test;
	
	private function __construct() {
		$this->test = test::getInstance();
	}
	
	public static function getInstance() {
		if (!isset(self::$instance)) {
			echo '<br>new test2 instance';
            $c = __CLASS__;
            self::$instance = new $c;
        }
       return self::$instance;
	}
	
}

print 'start:<br>';

for ($i = 1; $i < 10; $i++) {
	
	print '<br>loop: '.$i;
	$t = test::getInstance();
	
}


Expected result:
----------------
start:

loop: 1
new test instance
new tes2 instance
loop: 2
loop: 3
loop: 4
loop: 5
loop: 6
loop: 7
loop: 8
loop: 9



Actual result:
--------------
start:

loop: 1
new test instance
new test2 instance
new test instance
new test2 instance
new test instance
new test2 instance
new test instance
new test2 instance
new test instance
new test2 instance
...
till php runs out of memory


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52653&edit=1


Thread (3 messages)

« previous php.bugs (#153648) next »