Created
December 14, 2015 12:53
-
-
Save sotoz/02b8b887b9bb2b0494d8 to your computer and use it in GitHub Desktop.
Singleton PHP Database connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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