Advertisement
Serafim

Untitled

Jan 30th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. abstract class Model
  2. {
  3. private $_data = [];
  4. public function __construct($data)
  5. {
  6. $this->_data = $data;
  7. }
  8.  
  9. public function __get($var) { return $this->_data[$var]; }
  10.  
  11. protected static $pk = 'id';
  12. protected static $table = null;
  13. protected static $connection; // PDO Connection
  14.  
  15. public function find($pk)
  16. {
  17. $q = self::$connection->prepare('SELECT * FROM :table WHERE :column = :value');
  18. $res = $q->execute([':table' => static::$table, ':column' => static::$pk, ':value' => $pk]);
  19. $objects = []
  20. foreach ($res as $ob) {
  21. $objects[] = new self($ob);
  22. }
  23. return $objects;
  24. }
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. class User extends Model
  32. {
  33. protected static $table = 'users';
  34. }
  35. $users = User::find(1);
  36. echo $users[0]->login // возвращает поле логин
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement