Skip to content

Instantly share code, notes, and snippets.

@sotoz
Created December 14, 2015 12:53
Show Gist options
  • Save sotoz/02b8b887b9bb2b0494d8 to your computer and use it in GitHub Desktop.
Save sotoz/02b8b887b9bb2b0494d8 to your computer and use it in GitHub Desktop.
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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment