Introduction
IteratorAggregate interface extends abstract Traversable interface. It is implemented by a class to create external iterator. This interface introduces on abstract method called getIterator.
Syntax
IteratorAggregate extends Traversable {
/* Methods */
abstract public getIterator ( void ) : Traversable
}Methods
IteratorAggregate::getIterator — Retrieve an external iterator
This function has no parameters and returns an instance of an object implementing Iterator or Traversable.
IteratorAggregate Example
In following PHP script, a class that implements IteratorAggregate interface contains an array as a propertyThe getIterator() method returns ArrayIterator object out of this array. We can traverse the array using foreach loop.
Example
<?php
class myIterator implements IteratorAggregate {
public $arr;
public function __construct() {
$this->arr = array(10,20,30,40);
}
public function getIterator() {
return new ArrayIterator($this->arr);
}
}
$obj = new myIterator();
foreach($obj as $key => $value) {
echo $key ." =>" . $value . "\n";
}
?>Output
traversal of array property shows following result
0=>10 1=>20 2=>30 3=>40