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

(编辑:jimmy 日期: 2024/10/3 浏览:2)

本文实例讲述了PHP聚合式迭代器接口IteratorAggregate用法。分享给大家供大家参考,具体如下:

PHP IteratorAggregate又叫聚合式迭代器,它提供了创建外部迭代器的接口,接口摘要如下:

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

实现getIterator方法时必须返回一个实现了Iterator接口的类的实例。

例子说明:

<"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";
}
"htmlcode">
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迭代器会把对象或数组封装为一个可以通过foreach来操作的类,具体可参考SPL 迭代器相关介绍,感兴趣的朋友可参考本站https://www.jb51.net/article/43074.htm。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。