I'm working on an MVC project and I'm writing the code for get access to the database connection.
I have two files:
The first (PdodbLib.class.php) extends PDO
:
class PdodbLib extends PDO {
public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS) {
parent::__construct($DB_TYPE . ":host=" . $DB_HOST . ";dbname=" . $DB_NAME, $DB_USER, $DB_PASS);
}
}
The second (ModelLib.class.php) created the object:
abstract class ModelLib {
public function __construct() {
$this->db = new PdodbLib("mysql", "localhost", "test", "test", "my_pass_123");
}
}
If I want to perform a select for example in other file I do something like this:
$sql = $this->db->prepare("select * from users");
$sql->execute();
return $sql->fetchAll(PDO::FETCH_ASSOC);
I would like to know if that's a good practise or if it's better to do it in a different way.
PDO
which then limits the power ofPDO
in the first place... – Alex L Feb 18 at 5:28PdodbLib
class adds nothing substantial to thePDO
class. – Alex L Feb 20 at 1:35__construct()
without any other like someselect
helper function and so on? – Bultack Feb 20 at 18:00