博客列表 >拦截器、构造器实现数据库增删改查-php-28课7.25

拦截器、构造器实现数据库增删改查-php-28课7.25

希望
希望原创
2020年07月30日 13:10:19669浏览

1. 方法拦截器,属性重载或方法重载

  1. <?php
  2. // __get(), __set(), __isset(), __unset()用法
  3. // 1.__get($name):当外部访问一个不存在或无权限访问的属性时,自动调用
  4. class Product1
  5. {
  6. public function __get($name)
  7. {
  8. return $name;
  9. }
  10. }
  11. $obj1 = new Product1();
  12. echo $obj1->name;
  13. echo $obj1->price;
  14. echo '<hr>';
  15. // 2.__set()属性更新操作重定向
  16. class Product2
  17. {
  18. public function __set($name, $value)
  19. {
  20. echo $name. ':' .$value;
  21. }
  22. }
  23. $obj2 = new Product2();
  24. $obj2->username = '小芳';
  25. echo '<hr>';
  26. //3. __isset()检查属性
  27. class Product3
  28. {
  29. public $username = '小刘';
  30. public function __isget($name)
  31. {
  32. echo $name. '小李';
  33. return isset($this->$name);
  34. }
  35. }
  36. $obj3 = new Product3();
  37. var_dump(isset($obj3->username));
  38. echo '<hr>';
  39. var_dump(isset($obj3->price));
  40. // 4.__unset()删除属性
  41. class Product4
  42. {
  43. public $name1 = '小明';
  44. private $name2 = '小六';
  45. public function __unset($name)
  46. {
  47. echo $this->$name ,'<hr>';
  48. // 访问内部私有属性
  49. unset($this->$name);
  50. echo $this->$name ,'<hr>';
  51. }
  52. }
  53. $obj4 = new Product4();
  54. // 访问公共成员
  55. echo '<hr>';
  56. echo $obj4->name1;
  57. echo '<hr>';
  58. // 删除公共成员
  59. unset($obj4->name1);
  60. echo '<hr>';
  61. // 外部访问内部私有属性
  62. echo $obj4->name1;
  63. unset($obj4->name2);

2.实例演示查询构造器中的select(),inset(),update(),delete()方法

1.select()

  1. <?php
  2. // 方法委托/方法拦截器 实战: 数据库查询构造器(链式查询)
  3. // 查询类
  4. class Query
  5. {
  6. // 连接对象
  7. protected $db;
  8. // 数据表
  9. protected $table;
  10. // 字段列表
  11. protected $field;
  12. // 记录数量
  13. protected $limit;
  14. // 构造方法: 连接数据库
  15. public function __construct($dsn, $username, $password)
  16. {
  17. $this->connect($dsn, $username, $password);
  18. }
  19. // 连接数据库
  20. private function connect($dsn, $username, $password)
  21. {
  22. $this->db = new PDO($dsn, $username, $password);
  23. }
  24. // 设置默认的数据表名称
  25. public function table($table)
  26. {
  27. $this->table = $table;
  28. return $this;
  29. }
  30. // 设置默认的字段名称
  31. public function field($field)
  32. {
  33. $this->field = $field;
  34. return $this;
  35. }
  36. // 链式方法: 设置查询数量
  37. public function limit($limit)
  38. {
  39. $this->limit = $limit;
  40. return $this;
  41. }
  42. // 生成查询语句
  43. protected function getSql()
  44. {
  45. return sprintf('SELECT %s FROM %s LIMIT %s', $this->field, $this->table, $this->limit);
  46. }
  47. // 执行查询
  48. public function select()
  49. {
  50. return $this->db->query($this->getSql())->fetchAll(PDO::FETCH_ASSOC);
  51. }
  52. }
  53. // 数据据操作类
  54. class DB
  55. {
  56. // 静态方法委托
  57. public static function __callStatic($name, $args)
  58. {
  59. // 获取到查询类的对象: new Query()
  60. $dsn = 'mysql:host=localhost;dbname=phpedu';
  61. $username = 'root';
  62. $password = 'melinda123';
  63. $query = new Query($dsn, $username, $password);
  64. // 直接跳到Query类中的具体方法来调用
  65. return call_user_func([$query, $name], ...$args);
  66. }
  67. }
  68. $result = DB::table('users')
  69. // 字段列表
  70. ->field('id,name,email')
  71. ->limit(2)
  72. // 开始查询
  73. ->select();
  74. print_r($result);

2.inset()

  1. <?php
  2. //数据库插入构造器
  3. class Query
  4. {
  5. protected $pdo;
  6. protected $table;
  7. protected $field;
  8. protected $values;
  9. // 构造方法: 连接数据库
  10. public function __construct($dsn, $username, $password)
  11. {
  12. $this->connect($dsn, $username, $password);
  13. }
  14. private function connect($dsn, $username, $password)
  15. {
  16. $this->pdo = new PDO($dsn, $username, $password);
  17. }
  18. public function table ($table)
  19. {
  20. $this->table = $table;
  21. return $this;
  22. }
  23. public function field ($field)
  24. {
  25. $this->field = $field;
  26. return $this;
  27. }
  28. public function values ($values)
  29. {
  30. $this->values = $values;
  31. return $this;
  32. }
  33. // 生成插入语句
  34. protected function sql()
  35. {
  36. return sprintf(' INSERT INTO %s (%s) VALUES (%s) ', $this->table, $this->field, $this->values);
  37. }
  38. // 执行
  39. public function insert()
  40. {
  41. $this->pdo->query($this->sql());
  42. }
  43. }
  44. class DB
  45. {
  46. public static function __callStatic($name, $args)
  47. {
  48. $dsn = 'mysql:host=localhost;dbname=phpedu';
  49. $username = 'root';
  50. $password = 'melinda123';
  51. $query = new Query($dsn, $username, $password);
  52. return call_user_func([$query, $name], ...$args);
  53. }
  54. }
  55. $result = DB::table('users')
  56. -> field(`id,username,email,password`)
  57. -> values("'飞猪', '123@qq.com', 'fz123'")
  58. -> insert();
  59. echo $result ? '添加成功' :"添加失败";

3.update()

  1. <?php
  2. //数据库更新构造器
  3. class Query
  4. {
  5. protected $pdo;
  6. protected $table;
  7. protected $set;
  8. protected $field;
  9. protected $values;
  10. protected $where;
  11. // 构造方法: 连接数据库
  12. public function __construct($dsn, $username, $password)
  13. {
  14. $this->connect($dsn, $username, $password);
  15. }
  16. // 连接数据库
  17. private function connect($dsn, $username, $password)
  18. {
  19. $this->pdo = new PDO($dsn, $username, $password);
  20. }
  21. // 设置默认的数据表名称
  22. public function table ($table)
  23. {
  24. $this->table = $table;
  25. return $this;
  26. }
  27. public function set ($set)
  28. {
  29. $this->set = $set;
  30. return $this;
  31. }
  32. // 设置默认的字段名称
  33. public function field ($field)
  34. {
  35. $this->field = $field;
  36. return $this;
  37. }
  38. public function values ($values)
  39. {
  40. $this->values = $values;
  41. return $this;
  42. }
  43. public function where ($where)
  44. {
  45. $this->where = $where;
  46. return $this;
  47. }
  48. // 生成插入语句
  49. protected function sql ()
  50. {
  51. return sprintf("UPDATE %s SET %s =%s WHERE %s = %s", $this->field, $this->table, $this->values, $this->set, $this->where);
  52. }
  53. // 执行
  54. public function update ()
  55. {
  56. $this->pdo->query($this->sql());
  57. }
  58. }
  59. // 数据据操作类
  60. class DB
  61. {
  62. // 静态方法委托
  63. public static function __callStatic($name, $args)
  64. {
  65. $dsn = 'mysql:host=localhost;dbname=phpedu';
  66. $username = 'root';
  67. $password = 'melinda123';
  68. $query = new Query($dsn, $username, $password);
  69. return call_user_func([$query, $name], ...$args);
  70. }
  71. }
  72. $result = DB::table('users')
  73. ->set('username')
  74. ->values("'天猫'")
  75. ->field('id')
  76. ->where("'7'")
  77. ->update();
  78. echo $result ? '更新成功' :"更新失败";

4.delete()

  1. <?php
  2. //数据库删除构造器
  3. class Query
  4. {
  5. protected $pdo;
  6. protected $table;
  7. protected $field;
  8. protected $where;
  9. protected $values;
  10. // 构造方法: 连接数据库
  11. public function __construct($dsn, $username, $password)
  12. {
  13. $this->connect($dsn, $username, $password);
  14. }
  15. // 连接数据库
  16. private function connect($dsn, $username, $password)
  17. {
  18. $this->pdo = new PDO($dsn, $username, $password);
  19. }
  20. // 设置默认的数据表名称
  21. public function table ($table)
  22. {
  23. $this->table = $table;
  24. return $this;
  25. }
  26. // 设置默认的字段名称
  27. public function field ($field)
  28. {
  29. $this->field = $field;
  30. return $this;
  31. }
  32. public function where ($where)
  33. {
  34. $this->where = $where;
  35. return $this;
  36. }
  37. public function values ($values)
  38. {
  39. $this->values = $values;
  40. return $this;
  41. }
  42. // 生成插入语句
  43. protected function sql ()
  44. {
  45. return sprintf(' DELETE FROM %s WHERE %s = %s', $this->table, $this->field, $this->values);
  46. }
  47. // 执行
  48. public function delete()
  49. {
  50. return $this->pdo->query($this->sql());
  51. }
  52. }
  53. // 数据据操作类
  54. class DB
  55. {
  56. // 静态方法委托
  57. public static function __callStatic($name, $args)
  58. {
  59. $dsn = 'mysql:host=localhost;dbname=phpedu';
  60. $username = 'root';
  61. $password = 'melinda123';
  62. $query = new Query($dsn, $username, $password);
  63. return call_user_func([$query, $name], ...$args);
  64. }
  65. }
  66. $result = DB::table('users')
  67. ->field('id')
  68. ->values(32)
  69. ->delete();
  70. echo $result ? '删除成功' :'删除失败';
  • 总结:
  • 拦截器:也可以理解为属性重载或方法重载,当外部访问不存在或没有权限访问的属性或方法时,进行自动调用
  • 构造器:用构造方法来连接数据库,实现了查询和删除数据库的数据,但更新和插入检查不到原因在哪里出错
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议