1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I don't understand the benefit of creating these classes? Essentially, the only thing being done is placing a pretty wrapper around PDO which then limits the power of PDO in the first place... \$\endgroup\$ – Alex L Feb 18 '15 at 5:28
  • \$\begingroup\$ @AlexL I think that i don't understand what you're trying to say :/ \$\endgroup\$ – Bultack Feb 19 '15 at 19:28
  • \$\begingroup\$ What I'm saying is... Your classes are useless. I don't mean to sound mean, but your PdodbLib class adds nothing substantial to the PDO class. \$\endgroup\$ – Alex L Feb 20 '15 at 1:35
  • \$\begingroup\$ @AlexL Do you mean that's useless to have a unique function that's a __construct() without any other like some select helper function and so on? \$\endgroup\$ – Bultack Feb 20 '15 at 18:00
  • \$\begingroup\$ No, I'm trying to say that your class adds no value to the original PDO class. It does nothing helpful or advantageous. You'd be better off just using the PDO class and not your homemade class. \$\endgroup\$ – Alex L Feb 20 '15 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.