zoukankan      html  css  js  c++  java
  • PHP各种魔术方法测试

      1 <?php
      2     #自动加载方法,当new一个class时,若class未被引入,则会自动调用__autoload()方法
      3     function __autoload($className) {
      4         include $className . ".php";
      5     }
      6 
      7     class Son {
      8         private $name;
      9         
     10         public function __construct($name) {
     11             $this->name = $name;
     12         }
     13         
     14         #echo输出类时,自动调用此方法
     15         public function __toString() {
     16             return $this->name;
     17         }
     18     }
     19 
     20     class Man {
     21         private $name;
     22         private $age;
     23         private $son;
     24         
     25         public function __construct($name, $age, Son $son) {
     26             $this->name = $name;
     27             $this->age = $age;
     28             $this->son = $son;
     29         }
     30         
     31         #析构方法,当脚本结束或调用unset()时执行此方法
     32         public function __destruct() {
     33             echo "Destruct: I'm {$this->name}, I was killed at " . date("Y-m-d H:i:s") . "<br>";
     34         }
     35         
     36         public function __toString() {
     37             return "toString: I'm {$this->name}, and {$this->age} years old, my son is {$this->son}.<br>";
     38         }
     39         
     40         #当调用一个不存在的function时,自动调用此方法
     41         public function __call($funcName, $args) {
     42             echo "Call: You call func \"{$funcName}\" with args \"" . serialize($args) . "\" ? It's not existed.<br>";
     43         }
     44         
     45         #当调用一个不存在的static function时,自动调用此方法
     46         public static function __callStatic($funcName, $args) {
     47             echo "callStatic: You call static func \"{$funcName}\" with args \"" . serialize($args) . "\" ? It's not existed.<br>";
     48         }
     49         
     50         #当调用get而$varName不存在时,自动调用此方法
     51         public function __get($varName) {
     52             return "Get: You call the variable \"{$varName}\" ? It's not existed.<br>";
     53         }
     54         
     55         #当调用set而$varName不存在时,自动调用此方法
     56         public function __set($varName, $val) {
     57             echo "Set: You want to assign value \"{$val}\" to variable \"{$varName}\" ? It's not existd.<br>";
     58         }
     59         
     60         #当调用isset而$varName不存在时,自动调用此方法
     61         public function __isset($varName) {
     62             echo "Isset: Tell you, the variable \"{$varName}\" is not existed.<br>";
     63         }
     64         
     65         #当调用unset而$varName不存在时,自动调用此方法
     66         public function __unset($varName) {
     67             echo "Unset: Tell you, the variable \"{$varName}\" has never been existed.<br>";
     68         }
     69         
     70         #调用serialize()前,会先执行此方法,serialize()只序列化__sleep()方法返回的成员变量
     71         public function __sleep() {
     72             return array("name");
     73         }
     74         
     75         #调用unserialize()前,会先执行此方法
     76         public function __wakeup() {
     77             $this->age = "21";
     78         }
     79         
     80         #当将class当做function调用时,会执行此方法
     81         public function __invoke($var) {
     82             echo "You want to invoke class \"" . __CLASS__ . "\" as a func with args \"{$var}\"? No! That's wrong!<br>";
     83         }
     84         
     85         #调用clone时,会执行此方法
     86         public function __clone() {
     87             $this->son = clone $this->son;    #此处进行深复制,强制将对象型成员变量复制一份新拷贝,否则只是复制对象引用
     88         }
     89     }
     90     
     91     $man = new Man("Jack", 18, new Son("Dick"));
     92     $man->smoke("Camle cigarette");    #测试__call()
     93     Man::drink(array("juice", "water"));    #测试__callStatic()
     94     echo $man->wife;    #测试__get()
     95     $man->wife = "Rose";    #测试__set()
     96     isset($man->daughter);    #测试__isset()
     97     unset($man->daughter);    #测试__unset()
     98     echo serialize($man) . "<br>";    #测试__sleep()
     99     echo unserialize(serialize($man));    #测试__wakeup()
    100     $man("kiss");    #测试__invoke()
    101     $manNew = clone $man;    #测试__clone()
    102     echo $manNew;
    103     $woman = new Woman();    #测试__autoload()
    104 ?>

    页面输出

    Call: You call func "smoke" with args "a:1:{i:0;s:15:"Camle cigarette";}" ? It's not existed.
    callStatic: You call static func "drink" with args "a:1:{i:0;a:2:{i:0;s:5:"juice";i:1;s:5:"water";}}" ? It's not existed.
    Get: You call the variable "wife" ? It's not existed.
    Set: You want to assign value "Rose" to variable "wife" ? It's not existd.
    Isset: Tell you, the variable "daughter" is not existed.
    Unset: Tell you, the variable "daughter" has never been existed.
    O:3:"Man":1:{s:9:"Manname";s:4:"Jack";}
    toString: I'm Jack, and 21 years old, my son is .
    Destruct: I'm Jack, I was killed at 2012-08-11 20:00:34
    You want to invoke class "Man" as a func with args "kiss"? No! That's wrong!
    Destruct: I'm Jack, I was killed at 2012-08-11 20:00:34

  • 相关阅读:
    jstack使用教程
    频繁fullgc排查
    ubuntu添加ubuntu make
    Spring属性编辑器详解
    mysql 查看触发器,删除触发器
    mongodb启动不了:提示错误信息为 child process failed, exited with error number 100
    RedHat7 防火墙设置以及端口设置
    linux 设置静态IP方法
    linux 安装mongo
    mongo 介绍
  • 原文地址:https://www.cnblogs.com/zemliu/p/2633964.html
Copyright © 2011-2022 走看看