|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-08-20 13:54 UTC] t-bader at gmx dot net
[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
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 14:00:01 2025 UTC |
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