PHP各種魔術方法測試

來源:互聯網
上載者:User
  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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.