Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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->*(*)......
- }
- ?>
Add Comment
Please, Sign In to add comment