2023-01-13 268
PHP IteratorAggregate又叫聚合式迭代器,它提供了创建外部迭代器的接口,接口摘要如下:
IteratorAggregate extends Traversable {
abstract public Traversable getIterator ( void )
}
实现getIterator方法时必须返回一个实现了Iterator接口的类的实例。
例子说明:
<?php
/**
* 利用聚合式迭代器,并返回一个实现了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迭代器会把对象或数组封装为一个可以通过foreach来操作的类,具体可参考SPL 迭代器相关介绍,感兴趣的朋友可参考本站https://www.jb51.net/article/43074.htm。
以上所述是小编给大家介绍的PHP聚合式迭代器接口IteratorAggregate用法分析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/25323.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态