魔术方法(Magic methods)

  1. __construct() 类的构造函数

     class Test
     {
         private $name;
    
         public function __construct($name)
         {
             $this->name = $name;
         }
    
         public function sayHello()
         {
             return 'Hello ' . $this->name;
         }
     }
     $test = new Test('aaa');
     $result = $test->sayHello();
     echo $result;
    
  2. __destruct() 类的析构函数

     class Test
     {
         private $path;
         private $resource;
    
         public function __construct($path)
         {
             $this->path = $path;
             $this->resource = fopen($this->path, 'r');
         }
    
         public function getFileData()
         {
             $str = fread($this->resource, filesize($this->path));
             return $str;
         }
    
         public function __destruct()
         {
             fclose($this->resource);
         }
     }
    
     $test = new Test('./test.php');
     $result = $test->getFileData();
     echo $result;
    
  3. __call() 在对象中调用一个不可访问方法时调用

     class Test
     {
         public function __call($name, $args)
         {
             $class = __CLASS__;
             $count = count($args);
             $msg = "类:{$class}方法:{$name}不存在,传入参数:{$count}个。";
             return $msg;
         }
    
         public function getDateTime(int $time = 0)
         {
             $time = abs($time);
             $time = empty($time) ? time() : $time;
             $date_time = date('Y-m-d H:i:s', $time);
             return $date_time;
         }
     }
     $test = new Test();
     $result = $test->getDate(1501234567);
     echo $result;
    
  4. __callStatic() 在对象中静态调用一个不可访问方法时调用

     class Test
     {
         public static function __callStatic($name, $args)
         {
             $class = __CLASS__;
             $count = count($args);
             $msg = "类:{$class}方法:{$name}不存在,传入参数:{$count}个。";
             return $msg;
         }
    
         public function getDateTime(int $time = 0)
         {
             $time = abs($time);
             $time = empty($time) ? time() : $time;
             $date_time = date('Y-m-d H:i:s', $time);
             return $date_time;
         }
     }
     $result = Test::getDate(1501234567);
     echo $result;
    
  5. __get() 获得一个类的成员变量时调用

     class Test
     {
         private $name;
         private $age;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __get($property_name)
         {
             return isset($this->$property_name) ? $this->$property_name : null;
         }
     }
    
     $test = new Test('aaa', 20);
     echo $test->name;
    
  6. __set() 设置一个类的成员变量时调用

     class Test
     {
         private $name;
         private $age;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __get($property_name)
         {
             return isset($this->$property_name) ? $this->$property_name : null;
         }
    
         public function __set($property_name, $value)
         {
             $this->$property_name = $value;
         }
     }
    
     $test = new Test('aaa', 20);
     $test->name = 'bbb';
     echo $test->name;
    
  7. __isset() 当对不可访问属性调用isset()或empty()时调用

     class Test
     {
         private $name;
         private $age;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __isset($property_name)
         {
             return isset($this->$property_name);
         }
     }
    
     $test = new Test('aaa', 20);
     var_dump(isset($test->name));
    
  8. __unset() 当对不可访问属性调用unset()时被调用

     class Test
     {
         private $name;
         private $age;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __get($property_name)
         {
             return isset($this->$property_name) ? $this->$property_name : null;
         }
    
         public function __isset($property_name)
         {
             return isset($this->$property_name);
         }
    
         public function __unset($property_name)
         {
             unset($this->$property_name);
         }
     }
    
     $test = new Test('aaa', 20);
     if (isset($test->name)) {
         echo $test->name . PHP_EOL;
         unset($test->name);
     } else {
         echo '成员变量name已经销毁' . PHP_EOL;
     }
     if (isset($test->name)) {
         echo $test->name . PHP_EOL;
         unset($test->name);
     } else {
         echo '成员变量name已经销毁' . PHP_EOL;
     }
    
  9. __sleep() 执行serialize()时会先调用这个函数

     class Test
     {
         private $name;
         private $age;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __sleep()
         {
             //Do something.
             //$this->name = base64_encode($this->name);
             return ['name', 'age'];
         }
     }
    
     $test = new Test('aaa', 20);
     $result = serialize($test);
     echo $result . PHP_EOL;
     $result = unserialize($result);
     var_dump($result);
    
  10. __wakeup() 执行unserialize()时会先调用这个函数

     class Test
     {
         private $name;
         private $age;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __sleep()
         {
             //Do something like:
             $this->name = base64_encode($this->name);
             return ['name', 'age'];
         }
    
         public function __wakeup()
         {
             //Do something like:
             $this->name = base64_decode($this->name);
             return ['name', 'age'];
         }
     }
    
     $test = new Test('aaa', 20);
     $result = serialize($test);
     echo $result . PHP_EOL;
     $result = unserialize($result);
     var_dump($result);
    
  11. __toString() 对象被当成字符串时的回应方法

     class Test
     {
         public function __toString()
         {
             $class = __CLASS__;
             return "类{$class}被当成字符串使用";
         }
     }
    
     $test = new Test();
     echo $test;
    
  12. __invoke() 调用函数的方式调用一个对象时的回应方法

     class Test
     {
         public function __invoke()
         {
             $class = __CLASS__;
             return "类{$class}被当成函数使用";
         }
     }
    
     $test = new Test();
     $test();
    
  13. __set_state() 打印、调用、修改对象时此静态方法会被调用

     class Test
     {
         public $name;
         public $age;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __set_state($property_arr)
         {
             $test = new Tsest();
             foreach ($property_arr as $key => $val){
                 $test->$key = $val;
             }
         }
     }
    
     $test = new Test('aaa', 20);
     $test->name = 'bbb';
     $test->age = 18;
     var_export($test);
    
  14. __clone() 当对象复制完成时调用

     class Test
     {
         private $name;
         private $age;
         private $clone = false;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __clone()
         {
             $this->clone = true;
         }
     }
    
     $test = new Test('aaa', 20);
     var_export($test);
     $test1 = clone $test;
     var_export($test1);
    
  15. __debugInfo() 打印所需调试信息php-^5.6.0

     class Test
     {
         private $name;
         private $age;
    
         public function __construct($name, $age)
         {
             $this->name = $name;
             $this->age = $age;
         }
    
         public function __debugInfo() {
             $data['test'] = $this->name;
             return $data;
         }
     }
    
     $test = new Test('aaa', 20);
     var_dump($test);
    
  16. __autoload() 尝试加载可能定义了的类

     # /classes/Test.php
     class Test
     {
         public function getDateTime(int $time = 0)
         {
             $time = abs($time);
             $time = empty($time) ? time() : $time;
             $date_time = date('Y-m-d H:i:s', $time);
             return $date_time;
         }
     }
    
     # /autoload.php
     function __autoload($class)
     {
         $path = __DIR__ . "/classes/{$class}.php";
         if(is_file($path) && is_readable($path)){
             require_once $path;
         }
     }
    
     # /index.php
     require_once __DIR__ . "/autoload.php";
     $test = new Test();
     echo $test->getDateTime();
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-07-08 16:49:59

results matching ""

    No results matching ""