Blogger Information
Blog 63
fans 8
comment 8
visits 50182
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP大牛成长之路: 序列化与__sleep(),wakeup() + 匿名类实战
周Sir-BLOG
Original
627 people have browsed it

1、序列化__sleep(),wakeup()

  1. // 序列化
  2. $str1 = 'php.cn';
  3. $str2 = serialize($str1);
  4. $str3 = unserialize($str2);
  5. echo '序列化前:' . $str1 . '<br>';
  6. echo '序列化后:' . $str2 . '<br>';
  7. echo '反序列化:' . $str3;
  8. echo '<hr>';
  9. $arr1 = ['php.cn', 'PHP中文网', true, false, 1, 2, 3];
  10. $arr2 = serialize($arr1);
  11. $arr3 = unserialize($arr2);
  12. echo '序列化前:' . $arr1 . '<br>';
  13. echo '序列化后:' . $arr2 . '<br>';
  14. echo '反序列化:<br>';
  15. print_r($arr3);
  16. echo '<hr>';
  17. class Demo
  18. {
  19. public $name = 'Peter';
  20. public $age = 38;
  21. public $email = 'peter@php.cn';
  22. // 当外部序列化类实例的时候,自动调用它,用来隐藏一些属性
  23. // serialize($this)被调用的时候自动执行它
  24. public function __sleep(): array
  25. {
  26. return ['name', 'email'];
  27. }
  28. // 外部反序列化这个对象的时候,会被自动调用
  29. public function __wakeup()
  30. {
  31. // 在唤醒一个对象的时候,可以同步执行一些操作
  32. $this->email = 'peter@java.com';
  33. }
  34. }
  35. $demo = new Demo();
  36. // 序列化数据的目的是将数据进行传输或保存
  37. file_put_contents('obj.txt', serialize($demo));
  38. $str = file_get_contents('obj.txt');
  39. echo $str . '<hr>';
  40. // 反序列化
  41. // string ===> object
  42. $obj = unserialize($str);
  43. // echo gettype($obj);
  44. echo '姓名:' . $obj->name . ': 邮箱: ' . $obj->email . ', 年龄 : ', $obj->age;
  • 输出结果如下:

2、匿名类实战

2.1 非匿名类使用接口实现同时支持PDO与mysqli的Db类

  1. // 数据库接口,构造方法自动实例化数据库
  2. // $type为数据库类型,同时支持PDO与mysqli
  3. //...$params为连接参数
  4. interface iDb
  5. {
  6. public function __construct($type, ...$params);
  7. }
  8. class Db implements iDb
  9. {
  10. private $type;
  11. private $db;
  12. public function __construct($type, ...$params)
  13. {
  14. $this->type = $type;
  15. if ($type === 'PDO') {
  16. $this->db = new PDO($params[0], $params[1], $params[2]);
  17. } elseif ($type === 'MYSQLI') {
  18. $this->db = new mysqli($params[0], $params[1], $params[2], $params[3]);
  19. } else {
  20. return '未知数据库类型!';
  21. }
  22. }
  23. public function select()
  24. {
  25. if($this->type==='PDO'){
  26. return $this->db->query('select * from users limit 3')->fetchAll(PDO::FETCH_ASSOC);
  27. }elseif($this->type==='MYSQLI'){
  28. return $this->db->query('select * from users limit 2')->fetch_all(MYSQLI_ASSOC);
  29. }else{
  30. return '未知数据库类型!';
  31. }
  32. }
  33. public function insert()
  34. {
  35. return $this->db->query("INSERT `users` SET `username`='arwang666', `password`='arwang666';");
  36. }
  37. public function update()
  38. {
  39. return $this->db->query("UPDATE `users` SET `username`='PETER', `password`='PETER' WHERE `id`=6;");
  40. }
  41. public function delete()
  42. {
  43. return $this->db->query("DELETE FROM `users` WHERE `id`=6;");
  44. }
  45. }
  46. // 客户端代码:
  47. // 使用PDO:
  48. // $demo = new Db('PDO', 'mysql:host=localhost;dbname=php12', 'root', 'root');
  49. // 使用mysqli
  50. $demo = new Db('MYSQLI','localhost', 'root', 'root', 'php12');
  51. var_dump($demo->select());
  52. var_dump($demo->insert());
  53. var_dump($demo->update());
  54. var_dump($demo->delete());

2.1 匿名类使用接口实现同时支持PDO与mysqli的Db类

  1. // 数据库接口,构造方法自动实例化数据库
  2. // $type为数据库类型,同时支持PDO与mysqli
  3. //...$params为连接参数
  4. interface iDb
  5. {
  6. public function __construct($type, ...$params);
  7. }
  8. // 注意:实例化类的时候第一个为数据库类型:pdo或mysqli
  9. // PDO
  10. // $user = (new class ('PDO','mysql:host=localhost;dbname=php12', 'root', 'root') implements iDb{
  11. // MYSQLI
  12. $user = (new class ('MYSQLI','localhost', 'root', 'root', 'php12') implements iDb{
  13. private $type;
  14. private $db;
  15. public function __construct($type, ...$params)
  16. {
  17. $this->type = $type;
  18. if ($type === 'PDO') {
  19. $this->db = new PDO($params[0], $params[1], $params[2]);
  20. } elseif ($type === 'MYSQLI') {
  21. $this->db = new mysqli($params[0], $params[1], $params[2], $params[3]);
  22. } else {
  23. return '未知数据库类型!';
  24. }
  25. }
  26. public function select()
  27. {
  28. if($this->type==='PDO'){
  29. return $this->db->query('select * from users limit 3')->fetchAll(PDO::FETCH_ASSOC);
  30. }elseif($this->type==='MYSQLI'){
  31. return $this->db->query('select * from users limit 2')->fetch_all(MYSQLI_ASSOC);
  32. }else{
  33. return '未知数据库类型!';
  34. }
  35. }
  36. public function insert()
  37. {
  38. return $this->db->query("INSERT `users` SET `username`='arwang666', `password`='arwang666';");
  39. }
  40. public function update()
  41. {
  42. return $this->db->query("UPDATE `users` SET `username`='PETER', `password`='PETER' WHERE `id`=6;");
  43. }
  44. public function delete()
  45. {
  46. return $this->db->query("DELETE FROM `users` WHERE `id`=6;");
  47. }
  48. });
  49. // 增删改查只需要修改$user的对应的方法
  50. var_dump($user->select());

数据库仅为3个字段,输出结果请需要的同学复制代码自行测试。

总结:

  • 理解了序列化与反序列化+__sleep(),wakeup()的应用;
  • 匿名类以简单完成增删改查操作,但感觉用的应该比较少。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:我们学得比较多, 用得时候根据场景再定
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