欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP聚合式迭代器接口IteratorAggregate用法分析

 更新時(shí)間:2017年12月28日 10:30:46   作者:奔跑的碼農(nóng)  
這篇文章主要介紹了PHP聚合式迭代器接口IteratorAggregate用法,結(jié)合實(shí)例形式分析了聚合式迭代器接口IteratorAggregate的概念、功能、定義及使用方法,需要的朋友可以參考下

本文實(shí)例講述了PHP聚合式迭代器接口IteratorAggregate用法。分享給大家供大家參考,具體如下:

PHP IteratorAggregate又叫聚合式迭代器,它提供了創(chuàng)建外部迭代器的接口,接口摘要如下:

IteratorAggregate extends Traversable {
  abstract public Traversable getIterator ( void )
}

實(shí)現(xiàn)getIterator方法時(shí)必須返回一個(gè)實(shí)現(xiàn)了Iterator接口的類的實(shí)例。

例子說(shuō)明:

<?php
/**
 * 利用聚合式迭代器,并返回一個(gè)實(shí)現(xiàn)了Iterator接口的類的實(shí)例
 *
 * @author 瘋狂老司機(jī)
 */
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迭代器會(huì)把對(duì)象或數(shù)組封裝為一個(gè)可以通過(guò)foreach來(lái)操作的類,具體可參考SPL 迭代器相關(guān)介紹,感興趣的朋友可參考本站http://www.dbjr.com.cn/article/43074.htm

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論