nodirtyrockstar Posted October 4, 2012 Share Posted October 4, 2012 (edited) I have multiple scripts referring to one another and it is getting a little confusing with database calls. I stumbled upon the singleton method, and I am trying to use it to create a Database class that will make sure there is only one connection at a time. This is what I have: class Database{ //Store the single instance of Database private static $m_pInstance; private function __construct(){ $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } } public static function getInstance() { if(!self::$m_pInstance) { self::$m_pInstance = new Database(); } return self::$m_pInstance; } } My connection definitions are stored in a cfg.php file which is required by this script. For the most part I understand how this works, but not well enough to understand why it isn't working. Here is the reference to this database: $mysqli = Database::getInstance(); Can someone help me figure out what I am doing wrong? The connection fails when I run it. It works when I put the above reference into a comment and simply connect with this: $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } Thanks. Edited October 4, 2012 by nodirtyrockstar Quote Link to comment https://forums.phpfreaks.com/topic/269082-instantiating-singleton-pattern-to-contain-the-database-connection/ Share on other sites More sharing options...
nodirtyrockstar Posted October 4, 2012 Author Share Posted October 4, 2012 Well, I now see this is much more complex than I originally thought, and I am unable to edit or delete the first post...unless I am missing something. So please disregard. Quote Link to comment https://forums.phpfreaks.com/topic/269082-instantiating-singleton-pattern-to-contain-the-database-connection/#findComment-1382729 Share on other sites More sharing options...
nodirtyrockstar Posted October 5, 2012 Author Share Posted October 5, 2012 I actually got it all to work, so I decided to post it in case someone comes across this thread. I used the singleton pattern to extend mysqli. Here's my class: class Database extends mysqli{ private static $database; private function __construct(){ global $dbhost, $dbuser, $dbpass, $db; parent::__construct($dbhost, $dbuser, $dbpass, $db); if ($this->connect_error) { die('Connect Error (' . $this->connect_errno . ') ' . $this->connect_error); } } public static function getInstance() { if(!self::$database) { self::$database = new Database(); } return self::$database; } public function __clone() { die(__CLASS__ . ' class can\'t be instantiated. Please use the method called getInstance.'); } } Then you can reference the class like so: $mysqli = Database::getInstance(); Quote Link to comment https://forums.phpfreaks.com/topic/269082-instantiating-singleton-pattern-to-contain-the-database-connection/#findComment-1382767 Share on other sites More sharing options...
Christian F. Posted October 6, 2012 Share Posted October 6, 2012 This: global $dbhost, $dbuser, $dbpass, $db; is bad, at the very least use constants. Preferably you should be sending those details as (optional) parameters to the GetInstance () method, which then passes them on to the constructor. Quote Link to comment https://forums.phpfreaks.com/topic/269082-instantiating-singleton-pattern-to-contain-the-database-connection/#findComment-1383243 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.