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

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://www.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一区二区三区 | 91麻豆亚洲国产成人久久| 亚洲一区二区三区国色天香| 欧美大尺度影片在线观看| 欧美激情在线精品一区二区| 亚洲中文字幕少妇视频| 69人妻一区二区三区蜜桃| 国产乱来视频在线观看| 日韩不卡一区二区在线观看| 日本a级片免费在线观看| 久久热久久热精品视频| 国产夫妻自拍一级黄片| 日本激情精品在线观看| 中文日韩av在线免费播放| 性激烈的欧美三级男同| 国产三级三级精品久久| 亚洲欧美日韩国产一区| 欧美日韩中文字幕精品| av影片免费网址大全| 日韩一区二区三区无吗电影| 国语少妇高潮对白在线| 亚洲精品国产二区中文字幕| 免费久久人人爽人人爽| 中文国产人精品久久蜜桃| 日韩欧美中文字幕综合网| 日韩高清不卡在线视频|