0
I am investigating good implementations that allow easy adaptability for connection to my db. I would like to receive feed backs and suggestions for improvements to this design. As an example: how would you implement in this model more than a database, would it be better to use an abstract class for the database, if yes, how would you do it? I am in a project that has several phases, in the future there may be need to change/add DB’s and tables and would not like to have almost to redo the structure. Contents of the files:
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) {
...
}
...
what is in your understanding "scalable for connection to my db"
– chambelix
Be able to use this design if there are more implementations to do, other connections, other DB, etc...
– Miguel
then edit your question and change the word for adaptability because a scalable database system is one that allows you to grow in number of databases or tables in order to answer in number of accesses.
– chambelix
Edited, sorry for the misunderstanding
– Miguel
don’t need to apologize just thought to get a good answer you need to be precise and not leave the doubt on who can help you.
– chambelix
Miguel, could you give an example, the question seems very comprehensive and or based on opinions, at the risk of being closed. Edit it by adding examples and situations where your code doesn’t fit.
– KaduAmaral