I'm building a PHP application which has to interact with several structurally identical databases. I'd like to use a singleton factory to hand out connections to the databases and minimize the number of duplicate connections. I'd also like to wrap the database class with a few functions.
It would be extremely convenient if I could do all this in a single class. I tried using a singleton factory (it seemed like a good idea at the time), only to realize that it seems like has to return other classes to be useful. Is there an easy way to combine the singleton factory and database wrapping functionality, or should I just put the database wrapping functions in another class?
static private $instance = array();
private function __construct($name) {
switch ($name) {
//select db connection
}
$this->db = $this->getDb();
return;
}
protected function __clone() {
}
public static function singleton($name) {
if (!isset(self::$instance[$name])) {
$c = __CLASS__;
self::$instance[$name] = new $c($name);
}
return self::$instance[$name];
}
public function wrapperFunction() {
//stuff
}