1
\$\begingroup\$

I'm trying to wrap my head around OOP, and am fairly new to PHP in general, so please forgive me if the following makes you cringe.

I've decided to try and create a basic database wrapper using PDO. Here's what I've done so far.

class DB {
    protected $db_user = 'root';
    protected $db_pass = '';
    protected $db_host = 'localhost';
    protected $db_name = 'login';

    private $dbh;
    private $sth;

    public function __construct() {
        $this->dbh = new PDO('mysql:host='.$this->db_host.'; dbname='.$this->db_name, $this->db_user, $this->db_pass);
    }

    public function select($get, $table, $where){
        $this->sth = $this->dbh->prepare('SELECT '.$get.' FROM '.$table.' WHERE '.$where);
    }

    public function bindSTR($name, $replacement) {
        $this->sth->bindParam($name, $replacement, PDO::PARAM_STR);
    }

    public function execute(){
        $this->sth->execute();
    }

    public function fetch(){
        return $this->sth->fetch(PDO::FETCH_ASSOC);
    }
}

And here is the usage:

$DB = new DB();
$DB->select('user_id, email', 'users', 'email = :email');
$DB->bindSTR(':email', 'examp[email protected]');
$DB->execute();
$results = $DB->fetch();

Now before I go ahead and start adding more functionality to the class, can anyone give me some feedback?

Is this a good way to go? How can I improve the class? Any suggestions in general?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Is this a good way to go? How can I improve the class? Any suggestions in general?

Right now, your class doesn't really add any functionality to PDO. It basically just renames it's methods (to names other programmers will now have to get familiar with), and reduces it's flexibility and functionality.

The only advantage you gain currently is that you save typing SELECT, FROM, and WHERE. I don't think that this is worth it.

Misc

  • use spaces around . for increased readability.
  • sth is a pretty common name, so it isn't that bad, but something like selectStatement would be a lot clearer. And what does dbh stand for?
  • I would probably rename get to columns (and I would probably make it an array so it's easier to use).
\$\endgroup\$
2
  • \$\begingroup\$ Hi tim. Sorry for digging up such an old post. I actually wanted to ask you something but had to wait until I had enough rep to comment. I was thinking that the OP is abstracting away the details of the database access layer so that it can be replaced with something else if needed. So that could be one benefit of his/her class. What do you think? \$\endgroup\$ Dec 14, 2015 at 5:06
  • \$\begingroup\$ @Andrew not really. If you mean different SQL databases, PDO already offers this. And if you mean something else completely, then this is way too generic, and would still require a complete rewrite of the app if a different db layer is used (a DAO would be the solution for that). The only advantage I could possibly see is that it is easier to eg log stuff. But in that case, it would be easier to extend PDO, overwrite the desired method, log, and then call super. \$\endgroup\$
    – tim
    Dec 14, 2015 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.