迭代器接口IteratorAggregate 与类 ArrayIterator(转)
2013-04-06 21:04
429 查看
也许你很想使用foreach来遍历一个类中的属性,然而你却没有很好的方式来这么做。可能使用PHP中class的操作的方式能够帮助你实现一些,但是现在我想你有了更好的方式。通过继承接口:IteratorAggregate来实现。
示例
[php] view plaincopy
<?php
/**
* 测试接口的使用
*
* @author monkee
* @since 2011-08-15
*/
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) {
echo $obj->$key;
echo "\n";
}
?>
以上示例来自于PHP5.3手册
interface IteratorAggregate
IteratorAggregate是一个接口,通过对它的继承,你可以使用迭代器来对类里的pulibc属性进行操作。它只有一个接口方法,如下:
[php] view plaincopy
IteratorAggregate implements Traversable {
abstract public Traversable getIterator ();
}
通过返回数组迭代类来实现。
ArrayIterator类
ArrayIterator是SPL中Iterator中的一个类,它将对象或数组封装为一个可以通过foreach来操作的类。
[php] view plaincopy
<?php
/**
* 测试接口的使用
*
* @author monkee
* @since 2011-08-15
*/
class testClass{
public $a, $b, $c;
public function __construct($a, $b, $c){
list($this->a, $this->b, $this->c) = array($a, $b, $c);
}
}
$arrayObj = new ArrayIterator(array(
'a' => 'A',
'b' => 'B',
'c' => 'C'
));
foreach($arrayObj as $key => $value){
echo $arrayObj[$key];
}
$newObj = new testClass('A', 'B', 'C');
$classObj = new ArrayIterator($newObj);
foreach($classObj as $key => $value){
echo $classObj[$key];
}
?>
它的定义如下:
[php] view plaincopy
ArrayIterator implements Iterator , Traversable , ArrayAccess , SeekableIterator , Countable {
mixed current ( void )
mixed key ( void )
void next ( void )
void rewind ( void )
void seek ( int $position )
bool valid ( void )
}
示例
[php] view plaincopy
<?php
/**
* 测试接口的使用
*
* @author monkee
* @since 2011-08-15
*/
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) {
echo $obj->$key;
echo "\n";
}
?>
以上示例来自于PHP5.3手册
interface IteratorAggregate
IteratorAggregate是一个接口,通过对它的继承,你可以使用迭代器来对类里的pulibc属性进行操作。它只有一个接口方法,如下:
[php] view plaincopy
IteratorAggregate implements Traversable {
abstract public Traversable getIterator ();
}
通过返回数组迭代类来实现。
ArrayIterator类
ArrayIterator是SPL中Iterator中的一个类,它将对象或数组封装为一个可以通过foreach来操作的类。
[php] view plaincopy
<?php
/**
* 测试接口的使用
*
* @author monkee
* @since 2011-08-15
*/
class testClass{
public $a, $b, $c;
public function __construct($a, $b, $c){
list($this->a, $this->b, $this->c) = array($a, $b, $c);
}
}
$arrayObj = new ArrayIterator(array(
'a' => 'A',
'b' => 'B',
'c' => 'C'
));
foreach($arrayObj as $key => $value){
echo $arrayObj[$key];
}
$newObj = new testClass('A', 'B', 'C');
$classObj = new ArrayIterator($newObj);
foreach($classObj as $key => $value){
echo $classObj[$key];
}
?>
它的定义如下:
[php] view plaincopy
ArrayIterator implements Iterator , Traversable , ArrayAccess , SeekableIterator , Countable {
mixed current ( void )
mixed key ( void )
void next ( void )
void rewind ( void )
void seek ( int $position )
bool valid ( void )
}
相关文章推荐
- PHP聚合式迭代器接口IteratorAggregate用法分析
- 4.IteratorAggregate(聚合式迭代器)接口
- 迭代器java.util.Iterator接口
- PHP迭代器接口Iterator用法分析
- Java迭代器:iterator和iterable接口的区别
- PHP迭代器Iterator接口
- (32)Java学习笔记——集合框架 / Collection 接口 / Iterator 迭代器
- Iterator迭代器-ListIterator-Enumeration枚举类接口的特点和区别
- (Java)利用ListIterator(iterator 重复器/迭代器的子接口) 操作ArrayList
- PHP:迭代器(Iterator)接口实现
- 关于Iterator接口(迭代器)
- Java-Iterator(迭代器)接口和泛型
- Iterator(迭代器)接口之next() 与 hasNext()
- PHP Iterator(迭代器)接口
- JDK源码阅读之Iterator迭代器接口
- Enumeration接口与迭代器(Iterator)
- 3.Iterator(迭代器)接口
- Iterator(迭代器)接口 --对象循环遍历
- Iterator<E> 接口 迭代器
- 9.8-全栈Java笔记:Iterator(迭代器)接口