概述
在html' target='_blank'>面向?qū)ο?/u>編程中,PHP提供了一系列的魔術(shù)方法,這些魔術(shù)方法為編程提供了很多便利。PHP中的魔術(shù)方法通常以__(兩個下劃線)開始,并且不需要顯示的調(diào)用而是由某種特定的條件出發(fā)。
開始之前
在總結(jié)PHP的魔術(shù)方法之前先來定義兩個類,以便后邊示例使用:
?phpclass Device{ public $name,$battery,$data = [],$connection; protected function connect(){ $this- connection = resource echo $this- name. connected .PHP_EOL; protected function disconnect(){ $this- connection = null; echo $this- name. disconnected .PHP_EOL;}
Device類有四個成員屬性和兩個成員方法。
?php class Battery{ private $charge = 0; public function setCharge($charge){ $charge = (int)$charge; if($charge 0){ $charge = 0; }else if($charge 100){ $charge = 100; } $this- charge = $charge;}
Battery類有一個成員屬性和一個成員方法。
構(gòu)造函數(shù)和析構(gòu)函數(shù)
構(gòu)造函數(shù)和析構(gòu)函數(shù)分別在對象創(chuàng)建和銷毀時被調(diào)用。對象被“銷毀”是指不存在任何對該對象的引用,比如引用該對象的變量被刪除(unset)、重新賦值或腳本執(zhí)行結(jié)束,都會調(diào)用析構(gòu)函數(shù)。
__construct()
__construct()構(gòu)造函數(shù)是目前為止最經(jīng)常使用的函數(shù)。在創(chuàng)建對象時,可以在構(gòu)造函數(shù)中做一些初始化工作。可以為構(gòu)造函數(shù)定義任意多個參數(shù),只要在實例化時傳入對應個數(shù)的參數(shù)即可。構(gòu)造函數(shù)中出現(xiàn)的任何異常都會阻止對象的創(chuàng)建。
?phpclass Device{ public $name,$battery,$data = [],$connection; public function __construct(Battery $battery,$name){ $this- battery = $battery; $this- name = $name; $this- connect(); } protected function connect(){ $this- connection = resource echo $this- name. connected .PHP_EOL; } protected function disconnect(){ $this- connection = null; echo $this- name. disconnected .PHP_EOL;}
上面的示例代碼中,Device類的構(gòu)造函數(shù)為成員屬性賦值并且調(diào)用了connect()方法。
將構(gòu)造函數(shù)聲明為私有方法,可以防止在類外部創(chuàng)建對象,這在單利模式中經(jīng)常使用。
__desctruct()
析構(gòu)函數(shù)通常在對象被銷毀時調(diào)用,析構(gòu)函數(shù)不接收任何參數(shù)。經(jīng)常在析構(gòu)函數(shù)中執(zhí)行一些清理工作,比如關(guān)閉數(shù)據(jù)庫連接等。
__get()
魔術(shù)方法__get()在我們嘗試訪問一個不存在的屬性時會被調(diào)用。它接收一個參數(shù),該參數(shù)表示訪問屬性的名字,并且將該屬性的值返回。在上面的Device類里,有一個data屬性,該屬性就在這里就起了作用,如下面得代碼:
?php class Device{ public $name,$battery,$data = [],$connection; public function __construct(Battery $battery,$name){ $this- battery = $battery; $this- name = $name; $this- connect(); } protected function connect(){ $this- connection = resource echo $this- name. connected .PHP_EOL; protected function disconnect(){ $this- connection = null; echo $this- name. disconnected .PHP_EOL;$battery = new Battery();$device = new Device($battery, mac echo $device- //Notice: Undefined property: Device::$aaa
?phpheader( Content-type: text/html; charset=utf-8 class Device{ public $name,$battery,$data = [],$connection; public function __construct(Battery $battery,$name){ $this- battery = $battery; $this- name = $name; $this- connect(); public function __get($name){ if(array_key_exists($name,$this- data)){ return $this- data[$name]; return 屬性不存在 protected function connect(){ $this- connection = resource echo $this- name. connected .PHP_EOL; } protected function disconnect(){ $this- connection = null; echo $this- name. disconnected .PHP_EOL;}$battery = new Battery();$device = new Device($battery, mac echo $device- //macconnected 屬性不存在
該魔術(shù)方法最常用的地方就是通過創(chuàng)建一個“只讀”的屬性來擴展訪問控制。在上面的Battery類中,有一個私有屬性$charge,我們可以通過__get()魔術(shù)方法將該屬性擴展為在類外部可讀但不能修改。代碼如下:
?php class Battery { private $charge = 0; public function __get($name) { if(isset($this- $name)) { return $this- $name; return null;}
__set()
__set()魔術(shù)方法在我們嘗試修改一個不可訪問的屬性時會被調(diào)用,它接收兩個參數(shù),一個表示屬性的名字,一個表示屬性的值。示例代碼如下:
?phpheader( Content-type: text/html; charset=utf-8 class Device{ public $name,$battery,$data = [],$connection; public function __construct(Battery $battery,$name){ $this- battery = $battery; $this- name = $name; $this- connect(); public function __get($name){ if(array_key_exists($name,$this- data)){ return $this- data[$name]; return 屬性不存在 public function __set($name,$value){ $this- data[$name] = $value; } protected function connect(){ $this- connection = resource echo $this- name. connected .PHP_EOL; } protected function disconnect(){ $this- connection = null; echo $this- name. disconnected .PHP_EOL;}$battery = new Battery();$device = new Device($battery, mac $device- aaa = 哈哈 echo $device- //macconnected 哈哈鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選