-
-
Save TheBeachMaster/3ca29651998360f6e75d8fedb2e9eaad to your computer and use it in GitHub Desktop.
PHP OOP Database class using MySQLi and Singleton.
This file contains 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 DbConnector { | |
private $_connection; | |
private static $_instance; | |
private $_host = "host"; | |
private $_username = "username"; | |
private $_password = "password"; | |
private $_database = "dbname"; | |
private $_dbport = 3306; | |
public static function getInstance() { | |
if(!self::$_instance) { | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
$this->_connection = new mysqli($this->_host, $this->_username, | |
$this->_password, $this->_database, $this->_dbport); | |
if(mysqli_connect_error()) { | |
trigger_error("Fatal MySQL Connection Error " . mysql_connect_error(), | |
E_USER_ERROR); | |
} | |
} | |
private function __clone() { } | |
public function getConnection() { | |
return $this->_connection; | |
} | |
} | |
class SomeOtherClass | |
{ | |
$conn = DbConnector::getInstance(); | |
$db = $conn->getConnection(); | |
# use $db->*(*)...... | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment