Blogger Information
Blog 32
fans 0
comment 0
visits 27410
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP对象序列化和异常类匿名类
Yang_Sir
Original
766 people have browsed it

PHP中可以将类的对象进行序列化,转为可以保存和传输的字符串。
用户自定义异常处理类,以应对更多异常情况。
使用匿名类直接创建一次性对象,可以代替一些简单的完整类

1. 对象序列化

  • 使用serialize()函数序列化对象,__sleep()指定可以序列化的属性
  • 使用unserialize()还原对象,__wakeup()设置还原对象时自动完成的程序
  1. <?php
  2. //对象的序列化,__sleep()、__wakeup()
  3. class Merchant
  4. {
  5. public $name = '安心小区便利店';
  6. protected $num = '80108480A';
  7. private $updateDate = '15099098909';
  8. private $phone = '18093309099';
  9. protected $cardno = null;
  10. public function getUpdateDate()
  11. {
  12. return $this->updateDate;
  13. }
  14. //序列化时自动调用
  15. public function __sleep()
  16. {
  17. return ['name','num'];
  18. }
  19. //反序列化时自动调用
  20. public function __wakeup()
  21. {
  22. $this->updateDate = '2020-5-5';
  23. }
  24. }
  25. $merchant = new Merchant;
  26. //序列化对象,只序列化了name和num两个属性
  27. echo $str = serialize($merchant);
  28. //输出:O:8:"Merchant":2:{s:4:"name";s:21:"安心小区便利店";s:6:"*num";s:9:"80108480A";}
  29. //反序列化,自动调用了__wakeup(),为更新日期重新赋值
  30. $obj = unserialize($str);
  31. echo $obj->getUpdateDate();//2020-5-5

2. 异常类

  • PHP异常处理类Exception,抛出程序执行异常
  • 用户可以自定义异常处理类,需继承系统Exception
  1. //自定义异常信息处理
  2. class loginException extends Exception
  3. {
  4. public function __construct($message,$code){
  5. $this->code = $code;
  6. $this->message = $message;
  7. }
  8. //重写异常信息输出格式
  9. public function __toString()
  10. {
  11. return __CLASS__.':登录失败!['.$this->code.']:'.$this->message;
  12. }
  13. }
  14. try{
  15. $username = $_GET['username'];
  16. $password = $_GET['password'];
  17. if($username !='admin'){
  18. throw new loginException('用户名不存在',1);
  19. }elseif($password !='1234qwer'){
  20. throw new loginException('密码错误',2);
  21. }
  22. }catch(loginException $e){
  23. echo $e;
  24. }
  25. //访问地址传参?username=admin&password=12345
  26. //输出:loginException:登录失败![2]:密码错误
  27. //访问地址传参?username=sds&password=1234qwer
  28. //输出:loginException:登录失败![1]:用户名不存在

3.匿名类

  • 匿名类,php7开始支持的功能,使用new class{},直接完成类的创建和实例化
  1. //匿名类
  2. //匿名类实现接口功能
  3. interface iA{
  4. public function sum(...$a);
  5. }
  6. $res = (new class implements iA{
  7. public function sum(...$a){
  8. return array_sum($a);
  9. }
  10. })->sum(1,4,665,65);
  11. echo $res;//735
  12. //匿名类作为函数参数使用
  13. function field(object $obj,array $field){
  14. return $obj->toStr($field);
  15. }
  16. $field = ['id','name','adress'];
  17. echo field(new class{
  18. public function toStr(array $arr){
  19. $arr = array_map(function($itme){
  20. return $itme = '`'.$itme.'`';
  21. },$arr);
  22. return implode(',',$arr);
  23. }
  24. },$field);//`id`,`name`,`adress`
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!