Gumbo_Interface_Iterator

Iterator
   |
   --Gumbo_Interface_Iterator

Iterator Pattern Interface

The Iterator is an OOP method for looping through a list of data. The pattern consists of an Iterator and an IteratorAggregate (defined in PHP5). An Aggregate is responsible for creating and returning an Iterator to the program. The following code samples demonstrate the variety of ways to loop through an Iterator.

 $it = new Gumbo_Iterator (array (...));
 $it->rewind ();
 foreach ($it as $key=>$val) {
 	...
 }

 $it->rewind ();
 for ($it;$it->valid ();$it->next ()) {
 	$key = $it->key ();
 	$val = $it->current ();
 	...
 }

 $it->rewind ();
 while ($it->valid ()) {
 	$key = $it->key ();
 	$val = $it->current ();
 	...
 	$it->next ();
 }

An IteratorAggregate is responsible for creating an Iterator. This is done by defining an implementation to the single IteratorAggregate method. An aggregate works well inside a foreach() loop, in that it does not require a new variable to hold a reference to the Iterator, it will simply use the Aggregate method to retrieve the Iterator. The for(..) and while(..) loops require an Iterator variable reference.

 class ConcreteAggregate implements IteratorAggregate {
 	...
 	public function getIterator () {
 		return new Iterator (array (...)); // where array as data
 	}
 }

 // to loop through an aggregate
 $aggregate = new ConcreteAggregate ();
 foreach ($aggregate as $key=>$val) {
 	...
 }

The additional methods were defined for diagnostic purposes. They don't hold much use for an Iterator object, and may be removed. They work well if the programmer uses the Iterator directly.

Author(s): Michael Luster <mluster79@yahoo.com>
License:New BSD License
Copyright:Copyright (c) 2007, iBayou, Michael Luster
Link:http://sourceforge.net/projects/phpgumbo
Version:0.0.1

Interface Methods

public int getCurrent ( ) [line 111]

Returns the current element incremental number


[ Top ]
public array getKeys ( ) [line 99]

Returns the array of keys in the object


[ Top ]
public array getList ( ) [line 105]

Returns the full array list


[ Top ]
public int getTotal ( ) [line 117]

Returns the total number of elements in the Iterator


[ Top ]