7.13 魔术方法

在PHP中内置了很多以__两个下划线开头命名的方法,这些称之为魔术方法。

  • __sleep
    可以在使用 serialize() 序列化之前,指明会被序列化的属性,例如某个对象中的数据很大,但是只需要用到其中一部分,那么就可以用到 __sleep 了。

    // 定义类
    class Example{
      // 私有属性
      private $foo = [1, 2, 3];
      private $bar = ['aa', 'bbb', 'cccc'];
      // 在序列化之前调用
      public function __sleep(){
          // 只序列化 bar 属性
          return ["bar"];
      }
    }
    // 实例化
    $exam = new Example();
    
    // O:7:"Example":1:{s:12:"Examplebar";a:3:  {i:0;s:2:"aa";i:1;s:3:"bbb";i:2;s:4:"cccc";}}
    echo serialize($exam);
    
    // 如果没有 __sleep 输出的是:
    // O:7:"Example":2:{s:12:"Examplefoo";a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}s:12:"Examplebar";a:3:{i:0;s:2:"aa";i:1;s:3:"bbb";i:2;s:4:"cccc";}}
    
  • __wakeup
    在使用unserialize() 反序列化之前调用,可以预先执行一些准备操作,即便 __sleep() 指明了仅序列化部分数据,但反序列后仍然能获取所有数据

    // 定义类
    class Example{
      // 私有属性
      private $foo = [1, 2, 3];
      private $bar = [];
      // 构造函数
      public function __construct(array $array){
          $this->bar = $array;
      }
      // 在序列化之前调用
      public function __sleep(){
          // 只序列化 bar 属性
          return ["bar"];
      }
      // 在反序列化之前调用
      public function __wakeup(){
          echo '<br />';
      }
    }
    // 实例化
    $exam = new Example(['a', 'b', 'c']);
    
    // 序列化
    // string(80) "O:7:"Example":1:{s:12:"Examplebar";a:3:{i:0;s:1:"a";i:1;s:1:"b";i:2;s:1:"c";}}" 
    $data = serialize($exam);
    var_dump($data);
    
    // 返序列化
    // object(Example)#2 (2) { ["foo":"Example":private]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } ["bar":"Example":private]=> array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" } }
    var_dump(unserialize($data));
    
  • __toString

    当一个对象在执行字符串操作时应该返回什么值

    // 定义类
    class Example{
      // 字符串形态
      public function __toString(){
          return 'hello world';
      }
    }
    // 实例化
    $exam = new Example(['a', 'b', 'c']);
    
    // hello world
    echo $exam;
    
  • __invoke

    以函数形式调用已实例化的对象时,会执行该方法

    // 定义类
    class Example{
      // 函数式调用
      public function __invoke(string $str){
          return $str;
      }
    }
    // 实例化
    $exam = new Example();
    
    // hello world
    echo $exam('hello world');
    
  • __set_state
    执行 var_export() 时调用,该函数根据调用时第二个参数的值来决定输出或返回一个字符串形式的变量。

    // 定义类
    class Example{
      // 我的属性
      private $foo = 123;
      private $bar = 'abc';
      // var_export
      public static function __set_state(array $arr){
          return 'hello world';
      }
    }
    // 实例化
    $exam = new Example();
    
    // Example::__set_state(array( 'foo' => 123, 'bar' => 'abc', ))
    var_export($exam, false);
    
  • __debugInfo
    执行 var_dump() 时,返回该对象的属性集合

    // 定义类
    class Example{
      // 我的属性
      private $foo = 123;
      private $bar = 'abc';
      // var_dump
      public function __debugInfo(){
          return [
              'foo'   =>  $this->foo,
              'bar'   =>  $this->bar . ' test',
          ];
      }
    }
    // 实例化
    $exam = new Example();
    // object(Example)#1 (2) { ["foo"]=> int(123) ["bar"]=> string(8) "abc test" }
    var_dump($exam);
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,270评论 19 139
  • 把当前目录作为Root Document只需要这条命令即可:php -S localhost:3300 也可以指定...
    绚烂的时光阅读 4,119评论 0 1
  • Address:https://www.zybuluo.com/XiangZhou/note/208532 Exp...
    天蠍蒗漫阅读 13,922评论 2 55
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,737评论 18 399
  • 人的一生中会面临很多选择,这只是大大小小的选择中不起眼的一个,却是你目前最严峻的考验。我从来都不觉得读书好代表有出...
    我是一么阅读 1,634评论 1 2

友情链接更多精彩内容