Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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', '[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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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).
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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