Blogger Information
Blog 119
fans 3
comment 1
visits 94675
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
对象序列化、sleep(), wakeup()、异常类、匿名类相关知识
赵大叔
Original
701 people have browsed it

序列化

serialize();序列化:就是把不能直接存储的数据转换成可存储的数据。
unserialize();反序列化: 就是把序列化的数据,转换成我们需要的格式
__sleep(): 魔术方法,当序列化一个对象的时候自动调用
__wakeup(): 魔术方法,当反序列化的时候自动调用
__clone():当克隆对象的时候自动调用

演示代码:

  1. <?php
  2. // __sleep():当序列化一个对象的时候自动调用
  3. // __wackup():当反序列化的时候自动调用
  4. // __clone():当克隆对象的时候自动调用
  5. abstract class aBo
  6. {
  7. public $name = '佴总';
  8. public $salary = 20000;
  9. }
  10. class Con extends aBo
  11. {
  12. public $sex = '男';
  13. protected $age = 38;
  14. private $isMarried = false;
  15. protected static $nationality = '中国/CHINA';
  16. public function __get($name)
  17. {
  18. return $this->$name;
  19. }
  20. public function __sleep(): array
  21. {
  22. return ['name', 'sex', 'isMarried'];
  23. }
  24. public function __wakeup()
  25. {
  26. $this->name = '尹总';
  27. $this->age = 41;
  28. $this->sex = '保密';
  29. }
  30. public function __clone()
  31. {
  32. $this->salary = 0;
  33. }
  34. }
  35. $con = new Con();
  36. echo serialize($con);
  37. echo '<hr>';
  38. $str = serialize($con);
  39. var_dump(unserialize($str));
  40. echo '<hr>';
  41. //clone 克隆出新对象
  42. $con1 = clone $con;
  43. echo $con1->name, '----', $con1->salary;

效果图:

异常类

__toString():把对象当成字符串打印的时候调用

演示代码:

  1. <?php
  2. use Exception;
  3. class MyException extends Exception
  4. {
  5. public function __toString()
  6. {
  7. return <<<HEREDOC
  8. <style>
  9. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  10. td {border:1px solid black;padding: 5px;}
  11. tr:first-of-type {background-color:#eee;}
  12. tr:last-of-type td {color: coral;}
  13. </style>
  14. <table>
  15. <caption>异常信息</caption>
  16. <tr>
  17. <th>内容</th>
  18. <th>异常代码</th>
  19. <th>行号</th>
  20. </tr>
  21. <tr>
  22. <td>$this->message</td>
  23. <td>$this->code</td>
  24. <td>$this->line</td>
  25. </tr>
  26. </table>
  27. HEREDOC;
  28. }
  29. }
  30. try {
  31. if(true)throw new MyException('用户名错误', 404);
  32. }catch (MyException $a) {
  33. echo $a;
  34. }

效果图:

匿名类

匿名类: 只用一次,声明, 实例化,成员访问三合一

演示代码:

  1. <?php
  2. // 匿名类
  3. // 这个类只用一次, 将类的声明, 类的实例化,成员访问三合一
  4. echo (new class
  5. {
  6. public function get($name)
  7. {
  8. return '姓名: ' . $name;
  9. }
  10. })->get('马总') , '<hr>';
  11. // 应用场景: 实现接口部分功能, 仅用在当前项目中
  12. interface iDb
  13. {
  14. public function __construct(...$params);
  15. }
  16. $res = (new class ('mysql:host=localhost;dbname=phpedu', 'root', 'root') implements iDb {
  17. private $db = null;
  18. public function __construct(...$params)
  19. {
  20. $this->db = new PDO($params[0],$params[1],$params[2]);
  21. }
  22. public function select()
  23. {
  24. return $this->db
  25. ->query('SELECT id,name,position FROM staffs ' .' LIMIT 4')
  26. ->fetchAll(PDO::FETCH_ASSOC);
  27. }
  28. }
  29. )->select();
  30. printf('<pre>%s</pre>', print_r($res, true));

效果图:

总结:__sleep()``__wakeup()``__clone()这三个魔术方法之前忽略了的感觉,还是很的用的。

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