Jump to content

Instantiating Singleton Pattern To Contain The Database Connection


nodirtyrockstar

Recommended Posts

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 by nodirtyrockstar
Link to comment
Share on other sites

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();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.