Skip to content

Instantly share code, notes, and snippets.

@cythrawll
Last active February 7, 2019 12:38
Show Gist options
  • Save cythrawll/3020502 to your computer and use it in GitHub Desktop.
Save cythrawll/3020502 to your computer and use it in GitHub Desktop.
<?php
abstract class Dao {
protected $db;
public function __construct(PDO $db) {
$this->db = $db;
}
public function setDb(PDO $db) {
$this->db = $db;
}
public function getDb() {
return $this->db;
}
}
<?php
abstract class Entity {
public $id;
}
<?php
$db = new PDO(...);
$userDao = new UserDao($db);
$user = $userDao->getById($id);
<?php
class User extends Entity {
public $name;
public $password;
}
<?php
class UserDao extends Dao {
public function getById($id) {
$stmt = $this->db->query("SELECT id,name,password FROM users WHERE id=:id");
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetchObject('User');
$stmt->closeCursor();
return $user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment