您好,登錄后才能下訂單哦!
這篇文章主要介紹了php中魔術方法是什么,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
類中的魔術方法
PHP 魔術方法指的是在某些時刻會自動被調用的內置函數,它們以兩個連續的下劃線開頭。
類的構造函數,用于初始化對象,在對象實例化時自動運行
析構函數,用于在 php 運行終止時,釋放對象所占用的內存。析構函數是 php 的垃圾回收機制,使用棧結構,后進先出。
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>";
輸出結果如下所示
end of php file release Dell release Asus release MAC
可以發現析構函數在 php 文件執行結束之后才執行
類中用 protected 和 private 關鍵字定義的成員屬性或方法是無法通過對象的實例訪問的。__get() 方法會且僅會在對象的實例訪問 proctected 和 private 成員屬性 時自動執行 (訪問成員方法時不會,因為沒有意義)。
__get() 方法的意義在于將 proctected 和 private 成員屬性進行處理后輸出。
__get() 有且僅有一個輸入參數
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
可以看到,當訪問 public 成員屬性 price 時,__get()方法并沒有被調用。輸出 brand 時,我們使用了 md5 對其進行了加密處理,這種對封裝的成員屬性進行處理后輸出的用法就是 get 方法的意義所在。
__set($name, $value) 與用于給當前類中封裝的方法或屬性進行重新賦值或定義。
與 get 類似但不同的時,__set($name, $value)會在成員屬性被訪問賦值時自動執行,其中 $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>";
輸出結果
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 賦值時調用了 set , 而訪問屬性時,調用了 get 。
用于直接打印對象句柄,也就是說當我們使用 echo 加對象名時,__torsring()將會被自動調用
class computer{ function __tostring(){ return "This is a computer class"; } } $myComputer = new computer(); echo $myComputer;
如果沒有 __totring() 方法,我們是無法使用 echo+對象名,會出現 fatal error
當我們調用不存在的方法時,__call() 會自動執行,用于進行異常處理,并使程序繼續正常運行。
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";
輸出結果為
starting computer erro function: shutdown error param: Array ( [0] => 10 min [1] => 20 min ) here
我們可以看到,$method 返回了錯誤的函數名,而 arguments 返回了參數,最后輸出了 "here" 說明程序繼續正常運行。
clone 關鍵字用于復制對象,__clone() 方法實在克隆對象時自動調用的函數
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>";
輸出結果
My computer's name is pc1 A computer has been cloned My computer's name is pc1 His computer's name is pc2
我們看到用 = 號并不能復制對象,只是為對象添加了一個別名而已,這里 $myComputer 和 $youComputer 指向同一塊內存,修改了 $youComputer 的值相當于修改了 $myComputer 的值。
在實例化對象時,__autolaod() 會自動被調用,用于快速取得對應的類文件
<?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>"; }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“php中魔術方法是什么”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。