PHP之預(yù)定義接口詳解
在PHP中有好幾個(gè)預(yù)定義的接口,比較常用的四個(gè)接口(IteratorAggregate(聚合式aggregate迭代器Iterator)、Countable、ArrayAccess、Iterator)分別給大家詳細(xì)介紹下。
IteratorAggregate(聚合式aggregate迭代器Iterator)接口
IteratorAggregate extends Traversable {
abstract public Traversable getIterator(void)
}
這個(gè)接口實(shí)現(xiàn)了一個(gè)功能——?jiǎng)?chuàng)建外部迭代器,具體怎么理解呢,當(dāng)我們使用foreach對(duì)對(duì)象進(jìn)行便遍歷的時(shí)候,如果沒(méi)有繼承IteratorAggregate接口,遍歷的是對(duì)象中所有的public屬性(只能是public $var這種形式)。要是繼承了IteratorAggregate,會(huì)使用類中實(shí)現(xiàn)的getIterator方法返回的對(duì)象,這里要注意返回的一定要是一個(gè)Traversable對(duì)象或者擴(kuò)展自Traversable的對(duì)象,否則會(huì)拋出異常
//看個(gè)例子 class My{ private $_data = [ 'a' => '燕睿濤', 'b' => 'yanruitao', 'c' => 'LULU', ]; public function getIterator() { return new ArrayIterator($this->_data); } } $obj = new My; foreach ($obj as $key => $value) { echo "$key => $value\n"; } //輸出結(jié)果為空 class My implements IteratorAggregate { private $_data = [ 'a' => '燕睿濤', 'b' => 'yanruitao', 'c' => 'LULU', ]; public function getIterator() { return new ArrayIterator($this->_data); } } $obj = new My; foreach ($obj as $key => $value) { echo "$key => $value\n"; } //結(jié)果: a => 燕睿濤 b => yanruitao c => LULU
Countable接口
Countable {
abstract public int count(void)
}
這個(gè)接口用于統(tǒng)計(jì)對(duì)象的數(shù)量,具體怎么理解呢,當(dāng)我們對(duì)一個(gè)對(duì)象調(diào)用count的時(shí)候,如果函數(shù)沒(méi)有繼承Countable將一直返回1,如果繼承了Countable會(huì)返回所實(shí)現(xiàn)的count方法所返回的數(shù)字,看看下面的例子:
class CountMe { protected $_myCount = 3; public function count() { return $this->_myCount; } } $countable = new CountMe(); echo count($countable); //返回1 class CountMe implements Countable { protected $_myCount = 3; public function count() { return $this->_myCount; } } $countable = new CountMe(); echo count($countable); //返回3 ArrayAccess接口 ArrayAccess { abstract public boolean offsetExists(mixed $offset) abstract public mixed offsetGet(mixed $offset) public void offsetSet(mixed $offset, mixed $value) public void offsetUnset(mixed $offset) } class CountMe { protected $_myCount = 3; public function count() { return $this->_myCount; } } $countable = new CountMe(); echo count($countable); //返回1 class CountMe implements Countable { protected $_myCount = 3; public function count() { return $this->_myCount; } } $countable = new CountMe(); echo count($countable); //返回3
ArrayAccess接口
ArrayAccess {
abstract public boolean offsetExists(mixed $offset)
abstract public mixed offsetGet(mixed $offset)
public void offsetSet(mixed $offset, mixed $value)
public void offsetUnset(mixed $offset)
}
這個(gè)接口的作用是讓我們可以像訪問(wèn)數(shù)組一樣訪問(wèn)對(duì)象,這個(gè)怎么說(shuō)好呢,我猜其實(shí)就是php在詞法分析的時(shí)候如果碰到了數(shù)組的方式使用對(duì)象,就回去對(duì)象中查找是否有實(shí)現(xiàn)ArrayAccess如果有的話,進(jìn)行對(duì)應(yīng)的操作(set、unset、isset、get),這樣我們就可以在類里面放置一個(gè)array,讓類實(shí)現(xiàn)數(shù)組方式的基本操作,下面看個(gè)例子:
class myObj { } $obj = new myObj; $obj['name']; //Fatal error: Cannot use object of type myObj as array in class myObj implements ArrayAccess { public function offsetSet($offset, $value) { echo "offsetSet : {$offset} => {$value}\n"; } public function offsetExists($offset) { echo "offsetExists : {$offset}\n"; } public function offsetUnset($offset) { echo "offsetUnset : {$offset}\n"; } public function offsetGet($offset) { echo "offsetGet : {$offset}\n"; } } $obj = new myObj; $obj[1] = '燕睿濤'; isset($obj['name']); unset($obj['name']); $obj['yrt']; //輸出結(jié)果: offsetSet : 1 => 燕睿濤 offsetExists : name offsetUnset : name offsetGet : yrt class myObj implements ArrayAccess { private $_data = []; public function offsetSet($offset, $value) { $this->_data[$offset] = $value; } public function offsetExists($offset) { return isset($this->_data[$offset]); } public function offsetUnset($offset) { unset($this->_data[$offset]); } public function offsetGet($offset) { return $this->_data[$offset]; } } $obj = new myObj; $obj['yrt'] = '燕睿濤'; var_dump($obj['yrt']); var_dump(isset($obj['yrt'])); unset($obj['yrt']); var_dump(isset($obj['yrt'])); var_dump($obj['yrt']); //輸出: string(9) "燕睿濤" bool(true) bool(false) Notice: Undefined index: yrt //最后一個(gè)會(huì)報(bào)出Notice
上面的對(duì)象只能是基本的數(shù)組操作,連遍歷都不行,結(jié)合之前的IteratorAggregate可以進(jìn)行foreach:
class myObj implements ArrayAccess, IteratorAggregate { private $_data = []; public function getIterator() { return new ArrayIterator($this->_data); } ...... } $obj = new myObj; $obj['yrt'] = '燕睿濤'; $obj[1] = '燕睿濤'; $obj['name'] = '燕睿濤'; $obj['age'] = 23; foreach ($obj as $key => $value) { echo "{$key} => {$value}\n"; } //輸出: yrt => 燕睿濤 1 => 燕睿濤 name => 燕睿濤 age => 23
Iterator接口:
Iterator extends Traversable {
abstract public mixed current(void)
abstract public scalar key(void)
abstract public void next(void)
abstract public void rewind(void)
abstract public boolean valid(void)
}
可在內(nèi)部迭代自己的外部迭代器或類的接口,這是官方文檔給出的解釋,看著還是不好理解,其實(shí)我感覺(jué)這個(gè)接口實(shí)現(xiàn)的功能和trratorAggregate(文檔:創(chuàng)建外部迭代器接口,接口直接返回一個(gè)迭代器)類似,不過(guò)這個(gè)在類的定義里面自己實(shí)現(xiàn)了,看個(gè)例子:
class myObj implements Iterator{ private $_data = []; public function __construct(Array $arr) { $this->_data = $arr; } public function current() { return current($this->_data); } public function key() { return key($this->_data); } public function next() { next($this->_data); } public function rewind() { reset($this->_data); } public function valid() { return $this->key() !== NULL; } } $t = [ 'yrt' => '燕睿濤', 'name' => '燕睿濤', false, '燕睿濤' ]; $obj = new myObj($t); foreach ($obj as $key => $value) { echo "{$key} => ".var_export($value, true)."\n"; } //輸出: yrt => '燕睿濤' name => '燕睿濤' 0 => false 1 => '燕睿濤'
上面這個(gè)參考了鳥(niǎo)哥的一篇文章關(guān)于一筆試題(Iterator模式),不過(guò)鳥(niǎo)哥的那個(gè)判斷valid有點(diǎn)瑕疵,當(dāng)碰到值北來(lái)就是false的時(shí)候就會(huì)截?cái)?/p>
總結(jié)
說(shuō)了這么多好像還是沒(méi)有體會(huì)到他們的用處,建議看看Yii2的源碼,源碼里面大量使用了這些東西,看了之后,你會(huì)慢慢覺(jué)得“哦~好像還真是挺有用的。。。。”
以上就是本文全部介紹,希望大家喜歡。
相關(guān)文章
- 生成唯一ID的應(yīng)用場(chǎng)景非常普遍,如臨時(shí)緩存文件名稱,臨時(shí)變量,臨時(shí)安全碼等,uniqid()函數(shù)基于以微秒計(jì)的當(dāng)前時(shí)間,生成一個(gè)唯一的 ID。由于生成唯一ID與微秒時(shí)間關(guān)聯(lián),因此ID的唯一性非??煽?/div> 2015-11-11
Yii實(shí)現(xiàn)簡(jiǎn)單分頁(yè)的方法
這篇文章主要介紹了Yii實(shí)現(xiàn)簡(jiǎn)單分頁(yè)的方法,涉及Yii模型調(diào)用讀取數(shù)據(jù)及視圖操作相關(guān)技巧,需要的朋友可以參考下2016-04-04php解析xml 的四種簡(jiǎn)單方法(附實(shí)例)
下面小編就為大家?guī)?lái)一篇php解析xml 的四種簡(jiǎn)單方法(附實(shí)例)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07教你如何開(kāi)啟shopnc b2b2c 偽靜態(tài)
最近要給一個(gè)shopnc網(wǎng)站開(kāi)啟偽靜態(tài),用的是shopnc b2b2c,在網(wǎng)上搜索了好多shopnc開(kāi)啟偽靜態(tài)的方法,但都是針對(duì)shaopnc c2c的,沒(méi)有關(guān)于shopnc b2b2c的,最后終于找到了shopnc b2b2c怎么開(kāi)啟偽靜態(tài)。2014-10-10PHP--用萬(wàn)網(wǎng)的接口實(shí)現(xiàn)域名查詢功能
PHP用萬(wàn)網(wǎng)的接口實(shí)現(xiàn)域名查詢功能,需要的朋友可以了解下2012-12-12PHP curl模擬登錄帶驗(yàn)證碼的網(wǎng)站
最近接了個(gè)項(xiàng)目,其中有需求是要登錄帶驗(yàn)證碼的網(wǎng)站,獲取數(shù)據(jù),但是我們不可能人為的一直去記錄數(shù)據(jù),想通過(guò)自動(dòng)采集的方式進(jìn)行,下面小編給大家?guī)?lái)的相關(guān)代碼,對(duì)php curl 模擬登錄帶驗(yàn)證碼的網(wǎng)站感興趣的朋友一起學(xué)習(xí)吧2015-11-11php 升級(jí)到 5.3+ 后出現(xiàn)的一些錯(cuò)誤,如 ereg(); ereg_replace(); 函數(shù)報(bào)錯(cuò)
這篇文章主要介紹了php 升級(jí)到 5.3+ 后出現(xiàn)的一些錯(cuò)誤,如 ereg(); ereg_replace(); 函數(shù)報(bào)錯(cuò) 的相關(guān)資料,需要的朋友可以參考下2015-12-12最新評(píng)論