PHP聚合式迭代器接口IteratorAggregate用法分析
本文實例講述了PHP聚合式迭代器接口IteratorAggregate用法。分享給大家供大家參考,具體如下:
PHP IteratorAggregate又叫聚合式迭代器,它提供了創(chuàng)建外部迭代器的接口,接口摘要如下:
IteratorAggregate extends Traversable { abstract public Traversable getIterator ( void ) }
實現(xiàn)getIterator方法時必須返回一個實現(xiàn)了Iterator接口的類的實例。
例子說明:
<?php /** * 利用聚合式迭代器,并返回一個實現(xiàn)了Iterator接口的類的實例 * * @author 瘋狂老司機 */ class myData implements IteratorAggregate { public $one = "Public property one"; public $two = "Public property two"; public $three = "Public property three"; public function __construct() { $this->last = "last property"; } public function getIterator() { return new ArrayIterator($this); } } $obj = new myData; foreach($obj as $key => $value) { var_dump($key, $value); echo '<br>';// Linux:echo "\n"; } ?>
以上例子輸出:
string 'one' (length=3) string 'Public property one' (length=19) string 'two' (length=3) string 'Public property two' (length=19) string 'three' (length=5) string 'Public property three' (length=21) string 'last' (length=4) string 'last property' (length=13)
ArrayIterator迭代器會把對象或數(shù)組封裝為一個可以通過foreach來操作的類,具體可參考SPL 迭代器相關(guān)介紹,感興趣的朋友可參考本站http://www.dbjr.com.cn/article/43074.htm。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
用php來限制每個ip每天瀏覽頁面數(shù)量的實現(xiàn)思路
這篇文章主要介紹了用php來限制每個ip每天瀏覽頁面數(shù)量的實現(xiàn)思路,需要的朋友可以參考下2015-02-02PHP數(shù)據(jù)庫操作之基于Mysqli的數(shù)據(jù)庫操作類庫
Mysqli 是什么,我這里也不進行描述了。因為網(wǎng)上關(guān)于 Mysqli 的教程數(shù)不勝數(shù),我這里為大家介紹一款基于 Mysqli 的操作數(shù)據(jù)庫類(M.class.php)2014-04-04PHP定時任務(wù)獲取微信access_token的方法
這篇文章主要介紹了PHP定時任務(wù)獲取微信access_token的方法,涉及php基于curl動態(tài)獲取access_token及CentOS下crontab設(shè)置計劃任務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10探討如何使用SimpleXML函數(shù)來加載和解析XML文檔
本篇文章是對使用SimpleXML函數(shù)來加載和解析XML文檔進行了詳細的分析介紹,需要的朋友參考下2013-06-06解析php中array_merge與array+array的區(qū)別
本篇文章是對php中array_merge與array+array的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP 7安裝調(diào)試工具Xdebug擴展的方法教程
Xdebug提供了豐富的調(diào)試函數(shù),也可將Xdebug安裝配置為zend studio、editplus調(diào)試PHP的第三方插件,通過開啟自動跟蹤(auto_trace)和分析器功能,可以直觀的看到PHP源代碼的性能數(shù)據(jù),以便優(yōu)化PHP代碼,本文介紹了PHP 7安裝調(diào)試工具Xdebug擴展的方法,需要的朋友可以參考。2017-06-06