1 <?php
2#自動加載方法,當new一個class時,若class未被引入,則會自動調用__autoload()方法 3function __autoload($className) {
4include$className . ".php";
5 }
6 7class Son {
8private$name;
9 10publicfunction __construct($name) {
11$this->name = $name;
12 }
13 14#echo輸出類時,自動調用此方法 15publicfunction __toString() {
16return$this->name;
17 }
18 }
19 20class Man {
21private$name;
22private$age;
23private$son;
24 25publicfunction __construct($name, $age, Son $son) {
26$this->name = $name;
27$this->age = $age;
28$this->son = $son;
29 }
30 31#析構方法,當腳本結束或調用unset()時執行此方法 32publicfunction __destruct() {
33echo "Destruct: I'm {$this->name}, I was killed at " . date("Y-m-d H:i:s") . "<br>";
34 }
35 36publicfunction __toString() {
37return "toString: I'm {$this->name}, and {$this->age} years old, my son is {$this->son}.<br>";
38 }
39 40#當調用一個不存在的function時,自動調用此方法 41publicfunction __call($funcName, $args) {
42echo "Call: You call func \"{$funcName}\" with args \"" . serialize($args) . "\" ? It's not existed.<br>";
43 }
44 45#當調用一個不存在的static function時,自動調用此方法 46publicstaticfunction __callStatic($funcName, $args) {
47echo "callStatic: You call static func \"{$funcName}\" with args \"" . serialize($args) . "\" ? It's not existed.<br>";
48 }
49 50#當調用get而$varName不存在時,自動調用此方法 51publicfunction __get($varName) {
52return "Get: You call the variable \"{$varName}\" ? It's not existed.<br>";
53 }
54 55#當調用set而$varName不存在時,自動調用此方法 56publicfunction __set($varName, $val) {
57echo "Set: You want to assign value \"{$val}\" to variable \"{$varName}\" ? It's not existd.<br>";
58 }
59 60#當調用isset而$varName不存在時,自動調用此方法 61publicfunction __isset($varName) {
62echo "Isset: Tell you, the variable \"{$varName}\" is not existed.<br>";
63 }
64 65#當調用unset而$varName不存在時,自動調用此方法 66publicfunction __unset($varName) {
67echo "Unset: Tell you, the variable \"{$varName}\" has never been existed.<br>";
68 }
69 70#調用serialize()前,會先執行此方法,serialize()只序列化__sleep()方法返回的成員變量 71publicfunction __sleep() {
72returnarray("name");
73 }
74 75#調用unserialize()前,會先執行此方法 76publicfunction __wakeup() {
77$this->age = "21";
78 }
79 80#當將class當做function調用時,會執行此方法 81publicfunction __invoke($var) {
82echo "You want to invoke class \"" . __CLASS__ . "\" as a func with args \"{$var}\"? No! That's wrong!<br>";
83 }
84 85#調用clone時,會執行此方法 86publicfunction __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() 94echo$man->wife; #測試__get() 95$man->wife = "Rose"; #測試__set() 96isset($man->daughter); #測試__isset() 97unset($man->daughter); #測試__unset() 98echoserialize($man) . "<br>"; #測試__sleep() 99echounserialize(serialize($man)); #測試__wakeup()100$man("kiss"); #測試__invoke()101$manNew = clone$man; #測試__clone()102echo$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