Blogger Information
Blog 48
fans 0
comment 0
visits 34177
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
简单演示序列化和匿名类的应用场景(0728)
丶久而旧之丶
Original
689 people have browsed it

序列化和匿名类的演示

序列化和反序列化后自动连接数据库

序列化对象

  1. <?php
  2. class Demo
  3. {
  4. public $params = [];
  5. public $db;
  6. public function __construct($dsn, $username, $password)
  7. {
  8. $this->params['dsn'] = $dsn;
  9. $this->params['username'] = $username;
  10. $this->params['password'] = $password;
  11. $this->connect();
  12. }
  13. private function connect()
  14. {
  15. $this->db = new PDO(...array_values($this->params));
  16. }
  17. public function __sleep()
  18. {
  19. return ['params'];
  20. }
  21. }
  22. $demo = new Demo('mysql:host=localhost;dbname=user', 'root', 'root');
  23. file_put_contents('abc.txt', serialize($a));

反序列化对象(自动连接数据库)

  1. <?php
  2. class Demo
  3. {
  4. private $db;
  5. public function __wakeup()
  6. {
  7. $this->db = new PDO(...array_values($this->params));
  8. }
  9. public function select($sql)
  10. {
  11. return $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  12. }
  13. }
  14. // 反序列并自动连接数据库
  15. $demo = unserialize(file_get_contents('PDO.txt'));
  16. echo '<pre>' . print_r($demo->select('select * from apple limit 3'), true) . '</pre>';

匿名类(一次性,类的声明,实例,类的访问3合1)-阅后继焚

  1. <?php
  2. // 1.实现接口功能
  3. interface iDb
  4. {
  5. public function __construct($params);
  6. }
  7. // PDO查询
  8. echo '<pre>' . print_r((new class (['mysql:host=localhost;dbname=user', 'root', 'root']) implements iDb
  9. {
  10. public $db;
  11. public function __construct($params)
  12. {
  13. $this->db = new PDO(...$params);
  14. }
  15. function select($sql)
  16. {
  17. return $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  18. }
  19. })->select('select * from apple limit 3'), true) . '</pre>';
  20. // PDO新增
  21. echo '新增成功' . ((new class (['mysql:host=localhost;dbname=user', 'root', 'root']) implements iDb
  22. {
  23. public $db;
  24. public function __construct($params)
  25. {
  26. $this->db = new PDO(...$params);
  27. }
  28. function insert($sql)
  29. {
  30. return $this->db->query($sql)->rowCount();
  31. }
  32. })->insert("insert apple set `username`='超时空',`password`=sha1(111),`sex`='男'")) . '条数据<hr>';
  33. // PDO更新操作
  34. echo '更新成功' . ((new class (['mysql:host=localhost;dbname=user', 'root', 'root']) implements iDb
  35. {
  36. public $db;
  37. public function __construct($params)
  38. {
  39. $this->db = new PDO(...$params);
  40. }
  41. function update($sql)
  42. {
  43. return $this->db->query($sql)->rowCount();
  44. }
  45. })->update("update apple set `username`='光恶魔',`password`=sha1(111),`sex`='男' where `id`=51")) . '条数据<hr>';
  46. // PDO删除
  47. echo '删除成功' . ((new class (['mysql:host=localhost;dbname=user', 'root', 'root']) implements iDb
  48. {
  49. public $db;
  50. public function __construct($params)
  51. {
  52. $this->db = new PDO(...$params);
  53. }
  54. function delete($sql)
  55. {
  56. return $this->db->query($sql)->rowCount();
  57. }
  58. })->delete("delete from apple where `id`=22")) . '条数据<hr>';

总结

1.了解了序列化的一些应用
2.还没有理解到匿名类的应用场景(如果只用一次可以不用创建类直接写代码,如果用多次就直接创建一个类?)

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:因为在php中不存在类表达式的概念, 所以匿名类一定程度上起到这个作业, 不仅仅是一次性那么简单的, 匿名类可以当成值使用,可以当成函数的参数放在回调上使用, 场景非常丰富的
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post