Jak Posted November 27, 2006 Share Posted November 27, 2006 Hi, I have a class that I’m using as a singleton and for some reason its just not working at all. Basically my A class (the singleton) is created, and the constructor creates a new B object (from the B class). The B class needs to use variables from the A classes instance, so its constructor calls the instance of A, but then it all goes wrong.When the instance of A is called for the second time it still thinks it doesent exist (the if is true) and so creates another instance of A, therefore running the constructor again, which in turn creates B again and so on (it gets stuck in a loop). I have no idea why this is, has anyone got any ideas? Here is an example piece of code that demonstrates the problem (although here it actually returns an Internal Server Error rather than getting stuck in a loop):[code]<?php class A { static public function instance() { static $instance; if(!isset($instance)) $instance = new A(); return $instance; } public function __construct() { new B; } } class B { public function __construct() { A::instance(); } } A::instance();?>[/code]Thanks in advance, Jack Link to comment https://forums.phpfreaks.com/topic/28583-problem-with-singletons/ Share on other sites More sharing options...
btherl Posted November 27, 2006 Share Posted November 27, 2006 Parse error: parse error, unexpected T_STATIC, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /var/www/www.prioritysubmit.com/t.php on line 5 Link to comment https://forums.phpfreaks.com/topic/28583-problem-with-singletons/#findComment-130803 Share on other sites More sharing options...
Jak Posted November 27, 2006 Author Share Posted November 27, 2006 Hmm, i dont get that, i just get Internal Server Error. Anyway, line 5 is "static public function instance()" i dont see anything wrong with that, is there? Im pretty sure there's no syntax error, because if i remove the "A::instance();" line inside class B its all fine.Im on PHP 5.2 by the way. Link to comment https://forums.phpfreaks.com/topic/28583-problem-with-singletons/#findComment-130805 Share on other sites More sharing options...
btherl Posted November 27, 2006 Share Posted November 27, 2006 Sorry.. I forgot which version of PHP I was using :)Well, here with php 5.1.4 it gives me a segmentation fault. That's pretty bad. I altered the code to this:[code=php:0]<?php class A { static public function instance() { static $instance; if(!isset($instance)) { print "Created instance\n"; $instance = new A(); } else { print "Didn't create instance\n"; } return $instance; } public function __construct() { new B; } } class B { public function __construct() { A::instance(); } } print "About to call A::instance()\n"; A::instance();?>[/code]The output is:About to call A::instance()Created instanceCreated instance....Segmentation faultEdit: Oops.. I should have put the print earlier. Yes, it is looping. Link to comment https://forums.phpfreaks.com/topic/28583-problem-with-singletons/#findComment-130811 Share on other sites More sharing options...
btherl Posted November 27, 2006 Share Posted November 27, 2006 This may be your answer.."Static variables in functions that are class members do not work quite as you would expect - they are not initialised only once throughout the life cycle of the program, but once per every object created." Link to comment https://forums.phpfreaks.com/topic/28583-problem-with-singletons/#findComment-130812 Share on other sites More sharing options...
Jak Posted November 27, 2006 Author Share Posted November 27, 2006 Hmm, i dont really understand. I thought the whole point of the singleton pattern was that only one instance of the class should exist, so surley the "but once per every object" thing shouldnt matter, because there should only be one.Every example of a singleton in php ive seen has created it like this (or similar), yet it dosent seem to work. Im sure it has something to do with that fact that the class getting the instance a second time is being created by the instance the first time. If i do this:[code]<?php class A { public static function instance() { static $instance; if(!isset($instance)) $instance = new A(); return $instance; } public function __construct() { new B; } } class B { public function __construct() { //A::instance(); } } $c = A::instance(); $c->test = 1; $d = A::instance(); echo $d->test;?>[/code]It returns 1, as expected, so the singleton is working, just not when it is a child of something else. Is there any way to fix it, or is it a bug in PHP? Link to comment https://forums.phpfreaks.com/topic/28583-problem-with-singletons/#findComment-130889 Share on other sites More sharing options...
trq Posted November 27, 2006 Share Posted November 27, 2006 An Internal Server Error meens your browser isn't set to display server side error messages. Im assuming your using IE? its somewhere in the Advanced settings. Link to comment https://forums.phpfreaks.com/topic/28583-problem-with-singletons/#findComment-130901 Share on other sites More sharing options...
Jak Posted November 27, 2006 Author Share Posted November 27, 2006 Hey, someone helped me figure it out, for anyone thats interested, its because the first instance of A dosent exist until the constructor has finished running, and because B is created within the constructor when B tries to call the instance of A it dosent exist yet, and so it goes round and round. Link to comment https://forums.phpfreaks.com/topic/28583-problem-with-singletons/#findComment-130903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.