Andy-H Posted June 15, 2009 Share Posted June 15, 2009 If I have a database class which implements the singleton design and allow it to establish a connection in the constructor does it make a call to the classes' __CONSTRUCT() every time I call db::getInstance()->whatEver(); Thanks for any replies. Quote Link to comment https://forums.phpfreaks.com/topic/162245-solved-singleton-constructor/ Share on other sites More sharing options...
Mark Baker Posted June 15, 2009 Share Posted June 15, 2009 Only if the getInstance() method tells it to. class myClass { private static $_instance; public static function getInstance() { if (!isset(self::$_instance) || is_null(self::$_instance)) { self::$_instance = new myClass(); } return self::$_instance; } // function getInstance() function __construct() { echo 'called constructor<br />'; } // function __construct() } // class myClass myClass::getInstance(); // Should display "called constructor" myClass::getInstance(); // Should not display "called constructor" Quote Link to comment https://forums.phpfreaks.com/topic/162245-solved-singleton-constructor/#findComment-856266 Share on other sites More sharing options...
Andy-H Posted June 15, 2009 Author Share Posted June 15, 2009 Thanks, I'll give that a go lol =D Quote Link to comment https://forums.phpfreaks.com/topic/162245-solved-singleton-constructor/#findComment-856267 Share on other sites More sharing options...
Andy-H Posted June 15, 2009 Author Share Posted June 15, 2009 Yup, it only echo's constructor called once with four calls, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/162245-solved-singleton-constructor/#findComment-856271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.