Iterator is a PHP5 default interface that includes the public methods (to implement):
- mixed current ( ) // returns the current element in the loop ($key)
- scalar key ( ) // returns the current key in the loop ($val)
- void next ( ) // called for obtain the next element
- void rewind ( ) // rewind iterator to the 1st element (called at the beginning)
- boolean valid ( ) // check if the current position is valid
class MyIt implements Iterator
{
{
public function current() {}}
public function key() {}
public function next() {}
public function rewind() {}
public function valid() {}
Example (minimal):
We can create our class, keeping data in a private member and let these methods to access it.
We will use the php default function reset(), current(), next(), current() that easily allow to manage the element pointer in an array.
class A implements Iterator
{
$a = new A( array(1,2,3,4) );
foreach ($a as $k=>$v)
{
print " $k=>$v ";
}
//output: 0=>1 1=>2 2=>3 3=>4
{
private $ar = array();}
public function __construct($ar) { $this->ar = $ar; }
public function current() {return current($this->ar);}
public function key() {return key($this->ar);}
public function next() { next($this->ar);}
public function rewind() {reset($this->ar);}
public function valid() {return current($this->ar)!== false;}
$a = new A( array(1,2,3,4) );
foreach ($a as $k=>$v)
{
print " $k=>$v ";
}
//output: 0=>1 1=>2 2=>3 3=>4
Note: this is an example, we don't need to implement an iterator for a simple array. Pratically, we'll use an interator when we have different type of data (example: event calendar with a collection of events = customized objects).
IteratorAggregate
We've just built an implementation of an iterator for a class containing an array.
Now, let's suppose that we have a class B which manages his own array with other functionalities. If we want to use the previous iterator in the class B, we don't need to reimplement the interface Iterator.
It's enough to implement the class IteratorAggregate and his method getIterator() which returns a object which implements Iterator (the class A).
minimal example:
class B implements IteratorAggregate
{
{
private $bArray = array(6,7,8); //not a good/useful practice, but it's a minimal example !!}
public function getIterator()
{
return new A($this->bArray);
}
foreach (new B() as $k=>$v)
{
print " $k=>$v ";
} //0=>6 1=>7 2=>8
No comments:
Post a Comment