Blogger Information
Blog 39
fans 0
comment 0
visits 30654
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP:对象序列化与实战案例
Original
688 people have browsed it

一、sleep()和wakeup()的应用场景实例

  1. <?php
  2. // 一个案例演示sleep(), wakeuup()
  3. class User1
  4. {
  5. protected $name = '杨过';
  6. private $age = 28;
  7. }
  8. class User2 extends User1
  9. {
  10. protected $sex = '男';
  11. protected $skill = '黯然销魂掌';
  12. protected $partner = '小龙女';
  13. protected $crush = '郭襄';
  14. public function get()
  15. {
  16. return $this->name . '的妻子是' . $this->partner . '。';
  17. }
  18. }
  19. class User3 extends User2
  20. {
  21. public function __sleep()
  22. {
  23. return ['name','partner','crush'];
  24. }
  25. public function __wakeup()
  26. {
  27. $this->crush = '保密';
  28. }
  29. }
  30. $user2= new User2();
  31. echo $user2->get(),'<hr>';
  32. echo '对象User2 序列化:' . serialize($user2),'<hr>';
  33. $user3 = new User3();
  34. echo'对象User3 序列化:' . serialize($user3),'<hr>';
  35. echo '对象User3 反序列化:','<br>';
  36. $str = unserialize( serialize($user3));
  37. var_dump($str);

实例效果

二、模拟表单输入的异常处理

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>异常处理</title>
  7. </head>
  8. <body>
  9. <!-- 模拟一个表单输入界面 -->
  10. <form>
  11. <input type="text">
  12. <br/>
  13. <label for="">密码:</label>
  14. <input type="password">
  15. <br/>
  16. <label for="">邮箱:</label>
  17. <input type="mail">
  18. <br/>
  19. <input type="submit">
  20. </form>
  21. <?php
  22. // 自已创建一个操作的异常类
  23. class demoException extends Exception
  24. {
  25. public function __toString()
  26. {
  27. return <<< DBE
  28. <style>
  29. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  30. td {border:1px solid black;padding: 5px;}
  31. tr:first-of-type {background-color:#eee;}
  32. tr:last-of-type td {color: coral;}
  33. </style>
  34. <table>
  35. <tr><td>代码</td><td>信息</td><td>文件</td><td>行号</td></tr>
  36. <tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
  37. </table>
  38. DBE;
  39. }
  40. }
  41. // 异常处理
  42. try
  43. {
  44. throw new demoException('输入邮箱错误!',101);
  45. }catch (demoException $e){
  46. echo $e;
  47. }
  48. echo '<hr>';
  49. ?>
  50. </body>
  51. </html>

实例效果

总结:
1.序列化和反序列化可用于数据的保存、传输,比如数据库连接参数的保存和传输,实现自动连接数据库的功能。
序列化 调用 sleep()
反序列化 调用
wakeup()
2.__tostring在异常处理中可自定义出错信息。
我不清楚如何产生表单输入错误,并捕抓错误信息,我尝试输入不带@的邮箱,但没有错误信息,只能使用throw 语句模拟错误信息。

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