sotoz
12/14/2015 - 12:53 PM

Singleton PHP Database connection

Singleton PHP Database connection

<?php

class Connection {
	private $uname 		= "";
	private $password 	= "";
	private $host 		= "localhost";
	private $port;
	private $database	= "";
	private static $__instance = NULL;
	
	private function __construct() {
	}
	private function __clone() {
	}

	/**
	* Get a singleton instance of the connection class.
	* @return Connection instance
	*/
	public static function getInstance() {

		if (self::$__instance == NULL)
			self::$__instance = new Connection ();
		return self::$__instance;
	}
	/**
	 * Connect to the database.
	 * @return mysqli connection
	 */
	public function connect() {
		// connection to database here
		$connection = mysqli_connect($this->host, $this->uname, $this->password, $this->database);
		
		return $connection;
	}
}
 
?>