I've been investigating good implementations that allow easy adaptability to my db connection. I would like to receive feed backs and suggestions about improvements to this design. For example: how would you implement more than one database in this model, would it be better to use an abstract class for the database, if so, how would you do it? I am in a project that has several phases, in the future there may be a need to change / add DB's and tables and I do not want to have to redo the structure. File Contents:
config.php:
$db1Config = array(
"dbName" => "master_db",
"dbUser" => "master_user",
"dbPassword" => "master_pass",
"dbHost" => "master_host"
);
Class DB, DB.php:
class DB {
private static $_instance = Null;
private $_db = Null;
public function __construct($dbConfig) {
try {
$this->_db = new PDO('mysql:host=' .$dbConfig["dbHost"]. ';dbname=' .$dbConfig["dbName"], $dbConfig["dbUser"], $dbConfig["dbPassword"]);
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
$error = $e->getMessage();
//echo $error;
echo 'Updating database. Try again in a few moments. Sorry';
die();
}
}
public static function getInstance($dbConfig) {
if(!isset(self::$_instance)) {
self::$_instance = new DB($dbConfig);
}
return self::$_instance;
}
public function select_data($table, $where, $column, $upOrDown)
...
}
...
}
Class Service, Service.php
class Service {
private $_db = Null;
public function __construct($dbConfig) {
$this->_db = DB::getInstance($dbConfig);
}
public function service_exists($servId) {
$servExists = $this->_db->select_data('services', array('id', '=', $servId), Null, Null);
if (count($servExists) > 0) {
return True;
}
return False;
...
}
index.php
$service = new Service($db1Config);
if($service->service_exists($servId) {
...
}
...