Created
July 23, 2011 16:57
-
-
Save jamsesso/1101628 to your computer and use it in GitHub Desktop.
Class to manage multiple MySQL server connections in an application.
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 mysql extends mysqli | |
{ | |
public static $servers = array(); | |
public static function ignite_server($name, $host, $username, $password, $db, $port = null, $socket = null) | |
{ | |
if(is_null($port)) | |
$port = ini_get('mysqli.default_port'); | |
if(is_null($socket)) | |
$socket = ini_get('mysqli.default_socket'); | |
self::$servers[$name] = new mysqli($host, $username, $password, $db, $port, $socket); | |
if(self::$servers[$name]->connect_error) | |
throw new mysql_exception('Connection error. '.self::$servers[$name]->connect_error); | |
$GLOBALS[$name] = self::$servers[$name]; | |
return self::$servers[$name]; | |
} | |
public static function from_server($id) | |
{ | |
if(!isset(self::$servers[$id])) | |
throw new mysql_exception("Instance of server [$id] does not exist"); | |
return self::$servers[$id]; | |
} | |
public function __get($name) | |
{ | |
return self::from_server($name); | |
} | |
} | |
class mysql_exception extends exception | |
{ | |
public function __get($name) | |
{ | |
$method_chunks = explode('_', $name); | |
$fixed = 'get'; | |
foreach($method_chunks as $chunk) | |
$fixed.= ucfirst($chunk); | |
return $this->$fixed(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment