php聚合式迭代器的基礎(chǔ)知識點及實例代碼
說明
1、實現(xiàn)其他迭代器功能的接口,相當(dāng)于在其他迭代器上安裝一個外殼,只有一種方法。
2、聚合迭代器可以與許多迭代器結(jié)合,實現(xiàn)更高效的迭代。
實例
class MainIterator implements Iterator { private $var = array(); public function __construct($array) //構(gòu)造函數(shù), 初始化對象數(shù)組 { if (is_array($array)) { $this->var = $array; } } public function rewind() { echo "rewinding\n"; reset($this->var); //將數(shù)組的內(nèi)部指針指向第一個單元 } public function current() { $var = current($this->var); // 返回數(shù)組中的當(dāng)前值 echo "current: $var\n"; return $var; } public function key() { $var = key($this->var); //返回數(shù)組中內(nèi)部指針指向的當(dāng)前單元的鍵名 echo "key: $var\n"; return $var; } public function next() { $var = next($this->var); //返回數(shù)組內(nèi)部指針指向的下一個單元的值 echo "next: $var\n"; return $var; } public function valid() { return !is_null(key($this->var); //判斷當(dāng)前單元的鍵是否為空 } }
內(nèi)容擴展:
<?php class myData implements IteratorAggregate { public $property1 = "Public property one"; public $property2 = "Public property two"; public $property3 = "Public property three"; public function __construct() { $this->property4 = "last property"; } public function getIterator() { return new ArrayIterator($this); } } $obj = new myData; foreach($obj as $key => $value) { var_dump($key, $value); echo "\n"; } ?>
以上例程的輸出類似于:
string(9) "property1"
string(19) "Public property one"string(9) "property2"
string(19) "Public property two"string(9) "property3"
string(21) "Public property three"string(9) "property4"
string(13) "last property"
到此這篇關(guān)于php聚合式迭代器的基礎(chǔ)知識點及實例代碼的文章就介紹到這了,更多相關(guān)php聚合式迭代器是什么內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
學(xué)習(xí)php過程中的一些注意點的總結(jié)
在學(xué)習(xí)php的過程中會有一些細(xì)節(jié)是需要注意的,本文整理了一些比較實際的問題,希望對大家有所幫助2013-10-10php max_execution_time執(zhí)行時間問題
大部分PHP代碼執(zhí)行時間都不會很久。但是有些時候,比如等待圖片上傳,可能執(zhí)行時間過長導(dǎo)致超時。2011-07-07PHP模擬asp.net的StringBuilder類實現(xiàn)方法
這篇文章主要介紹了PHP模擬asp.net的StringBuilder類實現(xiàn)方法,較為簡單的模擬了StringBuilder類針對文本的基本操作技巧,需要的朋友可以參考下2015-08-08php上傳圖片到指定位置路徑保存到數(shù)據(jù)庫的具體實現(xiàn)
本文為大家介紹下php上傳圖片到指定位置路徑保存到數(shù)據(jù)庫的具體實現(xiàn),感興趣的朋友不要錯過2013-12-12PHP實現(xiàn)對文件鎖進(jìn)行加鎖、解鎖操作的方法
這篇文章主要介紹了PHP實現(xiàn)對文件鎖進(jìn)行加鎖、解鎖操作的方法,結(jié)合實例形式分析了PHP針對文件進(jìn)行加鎖、解鎖操作的功能、實現(xiàn)方法與相關(guān)注意事項,需要的朋友可以參考下2017-07-07