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