php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52653 multiple instances made with a singleton
Submitted: 2010-08-20 11:28 UTC Modified: 2010-08-20 14:02 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: paulscheltema at gmail dot com Assigned:
Status: Closed Package: Reproducible crash
PHP Version: 5.3.3 OS: debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: paulscheltema at gmail dot com
New email:
PHP Version: OS:

 

 [2010-08-20 11:28 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-20 13:54 UTC] 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 14:02 UTC] paulscheltema at gmail dot com
-Status: Open +Status: Closed
 [2010-08-20 14:02 UTC] paulscheltema at gmail dot com
Ok great, thanks for the help.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 06:01:29 2024 UTC