欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

php中魔術方法是什么-創(chuàng)新互聯(lián)

這篇文章主要介紹了php中魔術方法是什么,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)公司主營興山網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,app軟件開發(fā)公司,興山h5重慶小程序開發(fā)搭建,興山網(wǎng)站營銷推廣歡迎興山等地區(qū)企業(yè)咨詢

類中的魔術方法

PHP 魔術方法指的是在某些時刻會自動被調(diào)用的內(nèi)置函數(shù),它們以兩個連續(xù)的下劃線開頭。

類中的魔術方法

__construct()

類的構造函數(shù),用于初始化對象,在對象實例化時自動運行

__destruct()

析構函數(shù),用于在 php 運行終止時,釋放對象所占用的內(nèi)存。析構函數(shù)是 php 的垃圾回收機制,使用棧結(jié)構,后進先出。

構造函數(shù)和析構函數(shù)的例子

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)析構函數(shù)在 php 文件執(zhí)行結(jié)束之后才執(zhí)行

__get($name)

類中用 protected 和 private 關鍵字定義的成員屬性或方法是無法通過對象的實例訪問的。__get() 方法會且僅會在對象的實例訪問 proctected 和 private 成員屬性 時自動執(zhí)行 (訪問成員方法時不會,因為沒有意義)。

__get() 方法的意義在于將 proctected 和 private 成員屬性進行處理后輸出。

__get() 有且僅有一個輸入?yún)?shù)

__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()方法并沒有被調(diào)用。輸出 brand 時,我們使用了 md5 對其進行了加密處理,這種對封裝的成員屬性進行處理后輸出的用法就是 get 方法的意義所在。

__set($name, $value)

__set($name, $value) 與用于給當前類中封裝的方法或?qū)傩赃M行重新賦值或定義。

與 get 類似但不同的時,__set($name, $value)會在成員屬性被訪問賦值時自動執(zhí)行,其中 $name 是被訪問的成員屬性名,$value 為成員屬性被賦予的值

__set() 的例子

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 賦值時調(diào)用了 set , 而訪問屬性時,調(diào)用了 get 。

__tostring()

用于直接打印對象句柄,也就是說當我們使用 echo 加對象名時,__torsring()將會被自動調(diào)用

__tosring() 例子

class computer{     function __tostring(){         return "This is a computer class";     } } $myComputer = new computer(); echo $myComputer;

如果沒有 __totring() 方法,我們是無法使用 echo+對象名,會出現(xiàn) fatal error

__call($method, $arguments)

當我們調(diào)用不存在的方法時,__call() 會自動執(zhí)行,用于進行異常處理,并使程序繼續(xù)正常運行。

__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";

輸出結(jié)果為

starting computer
erro function: shutdown
error param: Array ( [0] => 10 min [1] => 20 min ) 
here

我們可以看到,$method 返回了錯誤的函數(shù)名,而 arguments 返回了參數(shù),最后輸出了 "here" 說明程序繼續(xù)正常運行。

__clone() 方法 和 clone 關鍵字

clone 關鍵字用于復制對象,__clone() 方法實在克隆對象時自動調(diào)用的函數(shù)

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>";

輸出結(jié)果

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 指向同一塊內(nèi)存,修改了 $youComputer 的值相當于修改了 $myComputer 的值。

__autolaod()

在實例化對象時,__autolaod() 會自動被調(diào)用,用于快速取得對應的類文件

__autoload() 例子

<?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中魔術方法是什么”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

新聞名稱:php中魔術方法是什么-創(chuàng)新互聯(lián)
文章位置:http://aaarwkj.com/article36/hoipg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供服務器托管、網(wǎng)站策劃、網(wǎng)站維護、建站公司、網(wǎng)站導航、App設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
精品国产熟女成人av| 久久久国产一区二区三区| 日本亚洲美丽少妇天堂| 在线看片国产精品自拍| 国产原创中文剧情性感av| 性生活的视频免费观看麻豆 | 精品在线免费视频观看| 国产熟女高潮一区二区| 亚洲国产午夜福利在线69| 亚洲欧美日韩国产亚洲欧美日韩国产| 国产福利传媒在线观看| 尤物视频精品在线观看| 加勒比中文字幕日本道| 先锋av一区二区三区| 18禁止看的视频免费| 亚洲精品中文字幕久久| 日韩亚洲欧美国产另类| 性知音国产精品粉色视频| 日本久久久精品福利视频| 亚洲黄色录像特级生活片| 国产综合精品一区二区| 丝袜美腿一区二区三区| 97国产精品亚洲精品| 国产女主播在线观看免费观看| 亚洲全乱码精品一区二区| 国产伦理免费精品中文字幕| 亚洲三区久久婷婷激情| 国产国产成人精品久久| 国产怡红院在线视频观看| 国产成人午夜视频免费一区| 亚洲午夜福利啪啪啪| 麻豆午夜视频免费在线观看| av在线免费观看美日韩| 精品亚洲天堂一区二区三区| 日韩精品一区二区三区中文| 色在线观看综合亚洲欧洲| 国产在线视频不卡一区| 午夜一区二区三区精品| 日韩人妻中文字幕亚洲| 一区二区三区在线观看淫| 国产成人av在线观看|