博客列表 >属性拦截器以及查询构造器

属性拦截器以及查询构造器

longlong
longlong原创
2020年07月29日 13:52:05511浏览

1. 属性拦截器

  1. <?php
  2. // 属性重载,又叫属性拦截器
  3. // 示例一:__get():读取不可访问属性的值时自动调用
  4. class Demo1
  5. {
  6. public function __get($name)
  7. {
  8. return $name.'<br>';
  9. }
  10. }
  11. $obj1 = new Demo1();
  12. echo $obj1->email;
  13. echo $obj1->age;
  14. echo '<hr>';
  15. // 示例二:__set():在给不可访问属性赋值时自动调用
  16. class Demo2
  17. {
  18. public function __set($name,$value)
  19. {
  20. echo $name.' : '.$value;
  21. }
  22. }
  23. $obj2 = new Demo2();
  24. $obj2->username = '小明';
  25. echo '<hr>';
  26. // 示例三:__isset():当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用
  27. class Demo3
  28. {
  29. public $username = '孙悟空';
  30. public function __isset($name)
  31. {
  32. echo $name.' 存在吗?'.'<br>';
  33. return isset($this->$name);
  34. }
  35. }
  36. $obj3 = new Demo3();
  37. var_dump(isset($obj3->username));
  38. echo '<hr>';
  39. var_dump(isset($obj3->email));
  40. echo '<hr>';
  41. // 示例四:__unset():当对不可访问属性调用 unset() 时,__unset() 会被调用
  42. class Demo4
  43. {
  44. public $num1 = 'aaa';
  45. private $num2 = 'bbb';
  46. public function __unset($name)
  47. {
  48. // 通过外部传参,可以输出私有成员$num2
  49. echo $this->$name,'<hr>';
  50. // 删除$num2变量
  51. unset($this->$name);
  52. // 现在就不能输出了,此时就能达到即使在外部也能访问私有成员,并操作它
  53. echo $this->$name,'<hr>';
  54. }
  55. }
  56. $obj4 = new Demo4();
  57. // 实例化类得到对象,访问类中公共成员,然后删除$num1变量,没有问题
  58. echo $obj4->num1,'<hr>';
  59. unset($obj4->num1);
  60. echo $obj4->num1,'<hr>';
  61. // 但是不能访问私有成员,更别说在外部删除私有变量了,但是可以通过unset()去自动调用类中的__unset()魔术方法
  62. // echo $obj4->num2;
  63. unset($obj4->num2);

2. 查询构造器

操作的数据表如下:

2.1 select查询

  1. <?php
  2. // SELECT `字段` FROM `数据表` LIMIT `几条记录`
  3. // 查询类
  4. class Query
  5. {
  6. // 1. 连接对象
  7. protected $pdo;
  8. // 2. 字段
  9. protected $field;
  10. // 3. 数据表名
  11. protected $table;
  12. // 4. 查询几条
  13. protected $limit;
  14. // 5. 连接数据库:使用构造方法
  15. public function __construct($dsn,$username,$password)
  16. {
  17. $this->connect($dsn,$username,$password);
  18. }
  19. private function connect($dsn,$username,$password)
  20. {
  21. $this->pdo = new PDO($dsn,$username,$password);
  22. }
  23. // 6. 设置默认查询字段。6,7,8步骤都设置return $this;是便于链式调用
  24. public function field ($field)
  25. {
  26. $this->field = $field;
  27. return $this;
  28. }
  29. // 7. 设置默认数据表名
  30. public function table ($table)
  31. {
  32. $this->table = $table;
  33. return $this;
  34. }
  35. // 8. 设置默认查询记录数量
  36. public function limit ($limit)
  37. {
  38. $this->limit = $limit;
  39. return $this;
  40. }
  41. // 9. 设置sql查询语句
  42. public function sql ()
  43. {
  44. return sprintf(' SELECT %s FROM %s LIMIT %s ', $this->field , $this->table , $this->limit);
  45. }
  46. // 10. 执行查询语句
  47. public function select ()
  48. {
  49. return $this->pdo->query($this->sql())->fetchAll(PDO::FETCH_ASSOC);
  50. }
  51. }
  52. // 数据库操作类
  53. class DB
  54. {
  55. public static function __callStatic ($name,$args)
  56. {
  57. $dsn = 'mysql:host=php.edu;dbname=first';
  58. $username = 'root';
  59. $password = 'root';
  60. $query = new Query($dsn,$username,$password);
  61. return call_user_func([$query,$name],...$args);
  62. }
  63. }
  64. $res = DB::table('student') -> field('username,sex,tel') -> limit(3) -> select();
  65. if (!empty($res)) {
  66. $table = <<<EOF
  67. <table border=1>
  68. <tr>
  69. <td>姓名</td>
  70. <td>性别</td>
  71. <td>电话</td>
  72. </tr>
  73. EOF;
  74. foreach ( $res as $key=>$value ) {
  75. $table.="<tr>";
  76. $table.="<td>{$value['username']}</td>";
  77. $table.="<td>{$value['sex']}</td>";
  78. $table.="<td>{$value['tel']}</td>";
  79. $table.="</tr>";
  80. }
  81. $table.="</table>";
  82. echo $table;
  83. }

2.2 insert插入

  1. <?php
  2. // INSERT INTO 数据表 (字段) VALUES (插入值)
  3. class Query
  4. {
  5. protected $pdo;
  6. protected $field;
  7. protected $table;
  8. protected $values;
  9. public function __construct($dsn,$username,$password)
  10. {
  11. $this->connect($dsn,$username,$password);
  12. }
  13. private function connect($dsn,$username,$password)
  14. {
  15. $this->pdo = new PDO($dsn,$username,$password);
  16. }
  17. public function field ($field)
  18. {
  19. $this->field = $field;
  20. return $this;
  21. }
  22. public function table ($table)
  23. {
  24. $this->table = $table;
  25. return $this;
  26. }
  27. public function values ($values)
  28. {
  29. $this->values = $values;
  30. return $this;
  31. }
  32. public function sql ()
  33. {
  34. return sprintf( " INSERT INTO %s (%s) VALUES (%s) ", $this->table , $this->field ,$this->values);
  35. }
  36. public function insert ()
  37. {
  38. $this->pdo->query($this->sql());
  39. }
  40. }
  41. class DB
  42. {
  43. public static function __callStatic ($name,$args)
  44. {
  45. $dsn = 'mysql:host=php.edu;dbname=first';
  46. $username = 'root';
  47. $password = 'root';
  48. $query = new Query($dsn,$username,$password);
  49. return call_user_func([$query,$name],...$args);
  50. }
  51. }
  52. DB::table('student') -> field('username,password,sex,age,tel') -> values("'哇哇','123456','男','50','15050355655'") -> insert();

2.3 update更新

  1. <?php
  2. // UPDATE 数据表 SET 字段1=value WHERE 字段2=where
  3. class Query
  4. {
  5. protected $pdo;
  6. protected $field;
  7. protected $set;
  8. protected $table;
  9. protected $value;
  10. protected $where;
  11. public function __construct($dsn,$username,$password)
  12. {
  13. $this->connect($dsn,$username,$password);
  14. }
  15. private function connect($dsn,$username,$password)
  16. {
  17. $this->pdo = new PDO($dsn,$username,$password);
  18. }
  19. public function set ($set)
  20. {
  21. $this->set = $set;
  22. return $this;
  23. }
  24. public function table ($table)
  25. {
  26. $this->table = $table;
  27. return $this;
  28. }
  29. public function value ($value)
  30. {
  31. $this->value = $value;
  32. return $this;
  33. }
  34. public function field ($field)
  35. {
  36. $this->field = $field;
  37. return $this;
  38. }
  39. public function where ($where)
  40. {
  41. $this->where = $where;
  42. return $this;
  43. }
  44. public function sql ()
  45. {
  46. return sprintf( " UPDATE %s SET %s = %s WHERE %s = %s", $this->table , $this->set , $this->value , $this->field , $this->where ) ;
  47. }
  48. public function update ()
  49. {
  50. // var_dump($this->pdo->query($this->sql()));
  51. $this->pdo->query($this->sql());
  52. }
  53. }
  54. class DB
  55. {
  56. public static function __callStatic ($name,$args)
  57. {
  58. $dsn = 'mysql:host=php.edu;dbname=first';
  59. $username = 'root';
  60. $password = 'root';
  61. $query = new Query($dsn,$username,$password);
  62. return call_user_func([$query,$name],...$args);
  63. }
  64. }
  65. DB::table('student') -> set('username') -> value("'老五'") -> field('id') -> where("'4'") -> update();

2.4 delete删除

  1. <?php
  2. // DELETE FROM 数据表 WHERE 字段=where
  3. class Query
  4. {
  5. protected $pdo;
  6. protected $field;
  7. protected $table;
  8. protected $where;
  9. public function __construct($dsn,$username,$password)
  10. {
  11. $this->connect($dsn,$username,$password);
  12. }
  13. private function connect($dsn,$username,$password)
  14. {
  15. $this->pdo = new PDO($dsn,$username,$password);
  16. }
  17. public function field ($field)
  18. {
  19. $this->field = $field;
  20. return $this;
  21. }
  22. public function table ($table)
  23. {
  24. $this->table = $table;
  25. return $this;
  26. }
  27. public function where ($where)
  28. {
  29. $this->where = $where;
  30. return $this;
  31. }
  32. public function sql ()
  33. {
  34. return sprintf( " DELETE FROM %s WHERE %s=%s ", $this->table , $this->field ,$this->where);
  35. }
  36. public function delete ()
  37. {
  38. $this->pdo->query($this->sql());
  39. }
  40. }
  41. class DB
  42. {
  43. public static function __callStatic ($name,$args)
  44. {
  45. $dsn = 'mysql:host=php.edu;dbname=first';
  46. $username = 'root';
  47. $password = 'root';
  48. $query = new Query($dsn,$username,$password);
  49. return call_user_func([$query,$name],...$args);
  50. }
  51. }
  52. DB::table('student') -> field('id') -> where("'1015'") -> delete();

3. 总结

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议