Blogger Information
Blog 60
fans 5
comment 3
visits 65109
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
序列化相关魔术方法以及匿名类小实战
longlong
Original
623 people have browsed it

1. __sleep()__wakeup() 魔术方法

  1. <?php
  2. // __sleep()和__wakeup 和 序列化相关
  3. // 1. 序列化可以序列化对象或数组
  4. // 2. 反序列化:返回序列化之前的原始结构
  5. // 3. 序列化主要用于数据的传输和保存
  6. // $arr = [1,2,'lisa',false,'abc'];
  7. // echo serialize($arr);
  8. // 将数据保存到文件中
  9. // file_put_contents('test.txt',serialize($arr));
  10. // 拿到数据
  11. // $str = file_get_contents('test.txt');
  12. // echo $str;
  13. // 还原数据
  14. // print_r(unserialize($str));
  15. // 序列化与魔术方法的应用
  16. class Demo
  17. {
  18. public $username = '校长';
  19. public $age = 60;
  20. public $gender = 'male';
  21. public $hobby = 'fish';
  22. // 外部序列化类实例时自动调用,可以用来隐藏一些属性
  23. public function __sleep()
  24. {
  25. // 只保存username和hobby,其他属性不会保存
  26. return ['username','hobby'];
  27. }
  28. // 外部反序列化对象时,自动调用,也可以同步一些操作
  29. public function __wakeup()
  30. {
  31. $this->hobby = 'Reading Book';
  32. }
  33. }
  34. // 实例化Demo
  35. $obj = new Demo();
  36. // 序列化
  37. // file_put_contents('demo.txt',serialize($obj));
  38. $demo = file_get_contents('demo.txt');
  39. echo $demo;
  40. echo '<hr>';
  41. // 反序列化
  42. $obj2 = unserialize($demo);
  43. echo $obj2->username.' 的爱好变为了 '.$obj2->hobby;

1.1 自定义小案例

  1. <?php
  2. // 案例:序列化时生成购物清单的总价格,反序列化时生成使用优惠券后的总价格
  3. class Demo
  4. {
  5. // 商品种类
  6. public $list1 = ['男士拖鞋','15'];
  7. public $list2 = ['垃圾桶','10'];
  8. public $list3 = ['台灯','80'];
  9. // 每种商品购买的数量
  10. public $num1;
  11. public $num2;
  12. public $num3;
  13. // 优惠券
  14. public $discount = 1;
  15. // 传入的参数表示每种商品买的数量
  16. public function __construct ($num1,$num2,$num3)
  17. {
  18. $this->num1 = $num1;
  19. $this->num2 = $num2;
  20. $this->num3 = $num3;
  21. }
  22. // 序列化类实例时
  23. public function __sleep()
  24. {
  25. echo '购物总金额是: '. ($this->list1[1]*$this->num1 + $this->list2[1]*$this->num2 + $this->list3[1]*$this->num3)*$this->discount.'<hr>';
  26. return ['list1','list2','list3','list4','discount','num1','num2','num3'];
  27. }
  28. // 反序列化时将优惠调至八折
  29. public function __wakeup()
  30. {
  31. $this->discount = 0.8;
  32. }
  33. }
  34. // 类实例
  35. $obj = new Demo(0,2,1);
  36. // 序列化
  37. $str = serialize($obj);
  38. // 反序列化
  39. $obj2 = unserialize($str);
  40. echo '优惠后 购物总金额是: '. ($obj2->list1[1]*$obj2->num1 + $obj2->list2[1]*$obj2->num2 + $obj2->list3[1]*$obj2->num3)*$obj2->discount;

2. 匿名类操作数据库

操作的数据库如下:

2.1 匿名类查询操作

  1. <?php
  2. // 匿名类查询操作
  3. interface iDB
  4. {
  5. public function __construct(...$params);
  6. }
  7. // 1. pdo方式
  8. $select = (new class ('mysql:host=php.edu;dbname=test','root','root') implements iDB {
  9. private $pdo;
  10. public function __construct (...$params)
  11. {
  12. $this->pdo = new PDO($params[0],$params[1],$params[2]);
  13. }
  14. public function select ()
  15. {
  16. $r = $this->pdo->query("SELECT `username`,`password`,`email` FROM `users`")->fetchAll(PDO::FETCH_ASSOC);
  17. $this->pdo = null;
  18. return $r;
  19. }
  20. }) -> select();
  21. foreach ($select as $v) {
  22. vprintf('| 用户名: %s | 密码: %s | 邮箱: %s <br>',$v);
  23. }
  24. echo '<hr>';
  25. // 2. mysqli方式
  26. $res = (new class ('php.edu','root','root','test') implements iDB {
  27. private $mysqli;
  28. public function __construct (...$params)
  29. {
  30. $this->mysqli = new mysqli($params[0],$params[1],$params[2],$params[3]);
  31. }
  32. public function select ()
  33. {
  34. $r = $this->mysqli->query("SELECT `username`,`password`,`email` FROM `users`")->fetch_all(MYSQLI_ASSOC);
  35. $this->mysqli->close();
  36. return $r;
  37. }
  38. }) -> select();
  39. foreach ($res as $v) {
  40. vprintf('| 用户名: %s | 密码: %s | 邮箱: %s <br>',$v);
  41. }

2.2 匿名类新增操作

  1. <?php
  2. // 匿名类新增数据到数据表
  3. interface iDB
  4. {
  5. public function __construct (...$params);
  6. }
  7. // 1. pdo方式
  8. $insert1 = (new class ('mysql:host=php.edu;dbname=test','root','root') implements iDB
  9. {
  10. private $pdo;
  11. public function __construct (...$params)
  12. {
  13. $this->pdo = new PDO($params[0],$params[1],$params[2]);
  14. }
  15. public function insert1 ()
  16. {
  17. $this->pdo->query("INSERT INTO `users` (`username`,`password`,`email`) VALUES ('哈哈',sha1('123456'),'haha@qq.com')");
  18. $this->pdo = null;
  19. }
  20. })->insert1();
  21. // 2. mysqli方式
  22. $insert2 = (new class ('php.edu','root','root','test') implements iDB
  23. {
  24. private $mysqli;
  25. public function __construct (...$params)
  26. {
  27. $this->mysqli = new mysqli($params[0],$params[1],$params[2],$params[3]);
  28. }
  29. public function insert2 ()
  30. {
  31. $this->mysqli->query("INSERT INTO `users` (`username`,`password`,`email`) VALUES ('小花',sha1('aaaaaa'),'xiaohua@qq.com')" );
  32. $this->mysqli->close();
  33. }
  34. })->insert2();

2.3 匿名类更新操作

  1. <?php
  2. // 匿名类更新操作
  3. interface iDB
  4. {
  5. public function __construct (...$params);
  6. }
  7. // 1. pdo方式
  8. $update1 = (new class ('mysql:host=php.edu;dbname=test','root','root') implements iDB
  9. {
  10. private $pdo;
  11. public function __construct (...$params)
  12. {
  13. $this->pdo = new PDO($params[0],$params[1],$params[2]);
  14. }
  15. public function update1 ()
  16. {
  17. $this->pdo->query("UPDATE `users` SET `username`='旺财',`email`='wangcai@qq.com' WHERE `id`=4");
  18. $this->pdo = null;
  19. }
  20. })->update1();
  21. // 2. mysqli方式
  22. $update2 = (new class ('php.edu','root','root','test') implements iDB
  23. {
  24. private $mysqli;
  25. public function __construct (...$params)
  26. {
  27. $this->mysqli = new mysqli($params[0],$params[1],$params[2],$params[3]);
  28. }
  29. public function update2 ()
  30. {
  31. $this->mysqli->query("UPDATE `users` SET `username`='牛郎',`email`='niulang@qq.com' WHERE `id`=3");
  32. $this->mysqli->close();
  33. }
  34. })->update2();

2.4 匿名类删除操作

  1. <?php
  2. // 匿名类删除操作
  3. interface iDB
  4. {
  5. public function __construct (...$params);
  6. }
  7. // 1. pdo方式
  8. $delete1 = (new class ('mysql:host=php.edu;dbname=test','root','root') implements iDB
  9. {
  10. private $pdo;
  11. public function __construct (...$params)
  12. {
  13. $this->pdo = new PDO($params[0],$params[1],$params[2]);
  14. }
  15. public function delete1 ()
  16. {
  17. $this->pdo->query("DELETE FROM `users` WHERE `id`=1");
  18. $this->pdo = null;
  19. }
  20. })->delete1();
  21. // 2. mysqli方式
  22. $delete2 = (new class ('php.edu','root','root','test') implements iDB
  23. {
  24. private $mysqli;
  25. public function __construct (...$params)
  26. {
  27. $this->mysqli = new mysqli($params[0],$params[1],$params[2],$params[3]);
  28. }
  29. public function delete2 ()
  30. {
  31. $this->mysqli->query("DELETE FROM `users` WHERE `username`='def'");
  32. $this->mysqli->close();
  33. }
  34. })->delete2();

3. 总结

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!