Blogger Information
Blog 56
fans 1
comment 0
visits 62716
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
__sleep及__wakeup使用实例及匿名类实战
零龙
Original
574 people have browsed it

__sleep

当一个对象被序列化后,会自动调用sleep方法,这个方法不接受参数,必须返回一个数组。
在执行之前首先serialize() 函数会首先检查是否存在一个魔术方法 sleep.如果存在,sleep()方法会先被调用, 然后才执行序列化操作。

__wakeup

反序列化一个对象后,php会自动调用wakeup,这个方法不需要参数,在反序列化时可以对类属性重赋值或隐藏。
示例

  1. <?php
  2. class User
  3. {
  4. public $name = '曹操';
  5. public $nation ='魏国';
  6. public $age = '58';
  7. public function __sleep()
  8. {
  9. return ['name','nation','age'];
  10. }
  11. //当外部序列化类实例的时候,会自动调用它,用来隐藏一些属性
  12. //serialize()被调用的时候自动执行它
  13. public function __wakeup()
  14. {
  15. $this->name ='刘备';
  16. $this->nation ='蜀国';
  17. }
  18. //外部反序列化的时候会被自动调用
  19. }
  20. $user = new User();
  21. //序列化数据的目的是将数据进行传输或保存
  22. file_put_contents('obj.txt',serialize($user));
  23. // 将类系列化后生成一个obj.txt文件保存
  24. $str = file_get_contents('obj.txt');
  25. //获取obj.txt文件赋值给$str
  26. echo '__sleep:序列化'.$str;
  27. echo "<hr>";
  28. // 反序列化
  29. $obj = unserialize($str);
  30. echo '__wakeup:姓名:'.$obj->name.' | 国家:'.$obj->nation.' | 年龄:'.$obj->age;

示例图:

匿名类实战

  1. <?php
  2. interface mDb
  3. {
  4. public function __construct(...$params);
  5. //实现数据库连接接口
  6. }
  7. $user = (new class('mysql:host=localhost;dbname=mysqli','root','142536') implements mDb
  8. {
  9. private $db = null;
  10. //定义一个空的链接
  11. public function __construct(...$params)
  12. {
  13. $this->db = new PDO($params[0],$params[1],$params[2]);
  14. //自动执行定义的私有属性赋值连接参数
  15. }
  16. public function select()
  17. {
  18. return $this->db->query('SELECT * FROM user LIMIT 1')->fetchAll(PDO::FETCH_ASSOC);
  19. }
  20. public function insert()
  21. {
  22. $sql ="INSERT `user` SET `username`='诸葛亮',`password`=sha1('142536'),`age`='38',`sex`='男',`email`='zgl@qq.com',`mobile`='13999999999'";
  23. return $this->db->query($sql) ? '添加成功':'添加失败';
  24. }
  25. public function update()
  26. {
  27. $sql ="UPDATE `user` SET `username`='刘备',`password`=sha1('142536'),`age`='42',`sex`='男',`email`='lb@qq.com',`mobile`='13777777777' WHERE id = 2";
  28. return $this->db->query($sql) ? '更新成功':'更新失败';
  29. }
  30. public function delete()
  31. {
  32. return $this->db->query('DELETE FROM `user` WHERE id>3') ? '删除成功':'删除失败';
  33. }
  34. }
  35. );
  36. print_r($user->select());
  37. echo "<hr>";
  38. print_r($user->insert());
  39. echo "<hr>";
  40. print_r($user->update());
  41. echo "<hr>";
  42. print_r($user->delete());

示例图:

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