Blogger Information
Blog 30
fans 0
comment 2
visits 29253
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP __sleep()与wakeup()实例演示、匿名类操作数据库
司马青衫
Original
766 people have browsed it

__sleep()wakeup()实例演示、匿名类操作数据库

__sleep()wakeup()实例演示

  • __sleep()当对对象进行serialize()序列化操作时,会调用类中的这个方法 然后将返回的数据进行序列化
  • __wakeup()当对序列化的对象进行unserialize()反序列化操作时,会调用类中的这个方法
  1. <?php
  2. class Test{
  3. private $name;
  4. private $age;
  5. public function __construct($n, $a){
  6. $this->name = $n;
  7. $this->age = $a;
  8. }
  9. public function __sleep(){
  10. //返回一个数值 里边的元素表示返回的属性名称
  11. return array('name');
  12. }
  13. public function __wakeup(){
  14. $this->age = 30;
  15. }
  16. }
  17. //实例化一个类
  18. $test = new Test('peter', 20);
  19. print_r($test);
  20. echo '<hr>';
  21. //序列化类对象
  22. $seriTest = serialize($test);
  23. print_r($seriTest);
  24. echo '<hr>';
  25. //反序列化
  26. $test = unserialize($seriTest);
  27. print_r($test);

匿名类操作数据库

  1. <?php
  2. //查找
  3. $users = (new class('localhost', 'root', 'root', 'db_users'){
  4. private $db = null;
  5. public function __construct(...$params){
  6. $this->db = new mysqli($params[0], $params[1], $params[2], $params[3]);
  7. }
  8. public function select(){
  9. return $this->db->query('SELECT * FROM `user` LIMIT 2')->fetch_all(MYSQLI_ASSOC);
  10. }
  11. })->select();
  12. print_r($users);
  13. echo '<hr>';
  14. //新增
  15. $username = 'pdo';
  16. $password = md5('pdo');
  17. $age = 10;
  18. $insertInfo = sprintf("username='%s', password='%s', age='%d'", $username, $password, $age);
  19. $affect_rows = (new class('localhost', 'root', 'root', 'db_users'){
  20. private $db;
  21. public function __construct(...$params){
  22. $this->db = new mysqli($params[0], $params[1], $params[2], $params[3]);
  23. }
  24. public function insert($info){
  25. $this->db->query(sprintf('INSERT `user` SET %s', $info));
  26. return $this->db->affected_rows;
  27. }
  28. })->insert($insertInfo);
  29. echo "新增了{$affect_rows}行数据<hr>";
  30. //更新
  31. $username = 'pdo';
  32. $password = md5('pdo');
  33. $age = 10;
  34. $updateInfo = sprintf("username='%s', password='%s', age='%d'", $username, $password, $age);
  35. $condition = 'id=63';
  36. $affect_rows = (new class('localhost', 'root', 'root', 'db_users'){
  37. private $db;
  38. public function __construct(...$params){
  39. $this->db = new mysqli($params[0], $params[1], $params[2], $params[3]);
  40. }
  41. public function update($info, $cond){
  42. $this->db->query(sprintf('UPDATE `user` SET %s WHERE %s', $info, $cond));
  43. return $this->db->affected_rows;
  44. }
  45. })->update($updateInfo, $condition);
  46. echo "更新了{$affect_rows}行数据<hr>";
  47. //删除
  48. $condition = 'id=103';
  49. $affect_rows = (new class('localhost', 'root', 'root', 'db_users'){
  50. private $db;
  51. public function __construct(...$params){
  52. $this->db = new mysqli($params[0], $params[1], $params[2], $params[3]);
  53. }
  54. public function delete($cond){
  55. $this->db->query(sprintf('DELETE FROM `user` WHERE %s', $cond));
  56. return $this->db->affected_rows;
  57. }
  58. })->delete($condition);
  59. echo "删除了{$affect_rows}行数据<hr>";

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