日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP各种魔术方法测试

發布時間:2024/4/15 php 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的PHP各种魔术方法测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。