您好,登錄后才能下訂單哦!
這篇文章主要介紹了php中魔術(shù)方法是什么,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
類中的魔術(shù)方法
PHP 魔術(shù)方法指的是在某些時(shí)刻會(huì)自動(dòng)被調(diào)用的內(nèi)置函數(shù),它們以兩個(gè)連續(xù)的下劃線開頭。
類的構(gòu)造函數(shù),用于初始化對(duì)象,在對(duì)象實(shí)例化時(shí)自動(dòng)運(yùn)行
析構(gòu)函數(shù),用于在 php 運(yùn)行終止時(shí),釋放對(duì)象所占用的內(nèi)存。析構(gòu)函數(shù)是 php 的垃圾回收機(jī)制,使用棧結(jié)構(gòu),后進(jìn)先出。
class computer{ private $brand; function __construct($brand){ $this->brand = $brand; } function __destruct(){ echo "release ".$this->brand."<br>"; } } $myComputer = new computer("MAC"); $yourComputer = new computer("Asus"); $hisComputer = new computer("Dell"); echo "end of php file<br>";
輸出結(jié)果如下所示
end of php file release Dell release Asus release MAC
可以發(fā)現(xiàn)析構(gòu)函數(shù)在 php 文件執(zhí)行結(jié)束之后才執(zhí)行
類中用 protected 和 private 關(guān)鍵字定義的成員屬性或方法是無法通過對(duì)象的實(shí)例訪問的。__get() 方法會(huì)且僅會(huì)在對(duì)象的實(shí)例訪問 proctected 和 private 成員屬性 時(shí)自動(dòng)執(zhí)行 (訪問成員方法時(shí)不會(huì),因?yàn)闆]有意義)。
__get() 方法的意義在于將 proctected 和 private 成員屬性進(jìn)行處理后輸出。
__get() 有且僅有一個(gè)輸入?yún)?shù)
class computer{ private $brand; protected $owner; public $price; function __construct($brand, $owner, $price){ $this->brand = $brand; $this->owner = $owner; $this->price = $price; } function __get($name){ echo "It's up to me to decide if let you konw the owner and the brand of this computer or not :)<br>"; echo "I will tell you the name of woner: ".$this->owner."<br>"; echo "I won't tell you that the brand is ".md5($this->brand)."<br>"; echo "<br>"; } function __destruct(){ echo "release ".$this->brand."<br>"; } } $myComputer = new computer("MAC", "me", "1000"); $yourComputer = new computer("Asus", "you", "500"); $hisComputer = new computer("Dell", "his", "700"); echo $myComputer->price; echo "<br><br>"; echo $myComputer->owner; echo $yourComputer->brand; echo "end of php file<br>";
輸出如下
1000 It's up to me to decide if let you konw the owner and the brand of this computer or not :) I will tell you the name of woner: me I won't tell you that the brand is 2e25c285356cbb0ed8785a1377027d79 It's up to me to decide if let you konw the owner and the brand of this computer or not :) I will tell you the name of woner: you I won't tell you that the brand is cb6ab3315634a1e4d11b091ba48b60ba end of php file release Dell release Asus release MAC
可以看到,當(dāng)訪問 public 成員屬性 price 時(shí),__get()方法并沒有被調(diào)用。輸出 brand 時(shí),我們使用了 md5 對(duì)其進(jìn)行了加密處理,這種對(duì)封裝的成員屬性進(jìn)行處理后輸出的用法就是 get 方法的意義所在。
__set($name, $value) 與用于給當(dāng)前類中封裝的方法或?qū)傩赃M(jìn)行重新賦值或定義。
與 get 類似但不同的時(shí),__set($name, $value)會(huì)在成員屬性被訪問賦值時(shí)自動(dòng)執(zhí)行,其中 $name 是被訪問的成員屬性名,$value 為成員屬性被賦予的值
class computer{ private $brand; protected $owner; function __construct($brand, $owner, $price){ $this->brand = $brand; $this->owner = $owner; $this->price = $price; } function __get($name){ echo "It's up to me to decide if let you konw the owner and the brand of this computer or not :)<br>"; echo "I will tell you the name of woner: ".$this->owner."<br>"; echo "I won't tell you that the brand is ".md5($this->brand)."<br>"; echo "<br>"; } function __set($name, $value){ $this->owner = $value; echo "set $name to $value"."<br><br>"; } function __destruct(){ echo "release ".$this->brand."<br>"; } } $myComputer = new computer("MAC", "me", "1000"); echo $myComputer->owner = "my friend"; echo $myComputer->owner; echo "end of php file<br>";
輸出結(jié)果
set owner to my friend my friendIt's up to me to decide if let you konw the owner and the brand of this computer or not :) I will tell you the name of woner: my friend I won't tell you that the brand is 2e25c285356cbb0ed8785a1377027d79 end of php file release MAC
我們看到在給 owner 賦值時(shí)調(diào)用了 set , 而訪問屬性時(shí),調(diào)用了 get 。
用于直接打印對(duì)象句柄,也就是說當(dāng)我們使用 echo 加對(duì)象名時(shí),__torsring()將會(huì)被自動(dòng)調(diào)用
class computer{ function __tostring(){ return "This is a computer class"; } } $myComputer = new computer(); echo $myComputer;
如果沒有 __totring() 方法,我們是無法使用 echo+對(duì)象名,會(huì)出現(xiàn) fatal error
當(dāng)我們調(diào)用不存在的方法時(shí),__call() 會(huì)自動(dòng)執(zhí)行,用于進(jìn)行異常處理,并使程序繼續(xù)正常運(yùn)行。
class computer{ function start(){ echo "starting computer<br>"; } function __call($m, $a){ echo "erro function: ".$m; echo "<br>"; echo "error param: "; print_r($a); echo "<br>"; } } $myComputer = new computer(); $myComputer->start(); $myComputer->shutdown('10 min', '20 min'); echo "here";
輸出結(jié)果為
starting computer erro function: shutdown error param: Array ( [0] => 10 min [1] => 20 min ) here
我們可以看到,$method 返回了錯(cuò)誤的函數(shù)名,而 arguments 返回了參數(shù),最后輸出了 "here" 說明程序繼續(xù)正常運(yùn)行。
clone 關(guān)鍵字用于復(fù)制對(duì)象,__clone() 方法實(shí)在克隆對(duì)象時(shí)自動(dòng)調(diào)用的函數(shù)
class computer{ public $name; function __clone(){ echo "A computer has been cloned<br>"; } } $myComputer = new computer(); $youComputer = $myComputer; $youComputer->name = 'pc1'; echo "My computer's name is ".$myComputer->name."<br>"; echo "<br>"; $hisComputer = clone $myComputer; $hisComputer->name = 'pc2'; echo "My computer's name is ".$myComputer->name."<br>"; echo "His computer's name is ".$hisComputer->name."<br>";
輸出結(jié)果
My computer's name is pc1 A computer has been cloned My computer's name is pc1 His computer's name is pc2
我們看到用 = 號(hào)并不能復(fù)制對(duì)象,只是為對(duì)象添加了一個(gè)別名而已,這里 $myComputer 和 $youComputer 指向同一塊內(nèi)存,修改了 $youComputer 的值相當(dāng)于修改了 $myComputer 的值。
在實(shí)例化對(duì)象時(shí),__autolaod() 會(huì)自動(dòng)被調(diào)用,用于快速取得對(duì)應(yīng)的類文件
<?php function __autoload($class_name) { include $class_name . '.php'; } $obj = new MyClass1(); $obj2 = new MyClass2(); ?>
帶 try, catch 異常處理的例子
function __autoload($class_name){ echo "want to load ".$class_name."<br>"; if(file_exists($class_name.".class.php")){ include($class_name.".class.php"); }else{ throw new Exception("Unable to laod ".$class_name.".class.php"); } } try{ $obj = new myClass(); } catch(Exception $e){ echo $e->getMessage()."<br>"; }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“php中魔術(shù)方法是什么”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。