Iterator implementation in PHP

Here is a simple iterator interface definition for PHP 4. Please note that this interface doesn't complay with the SPL (Standard PHP Library).

Solution

/**
 * Define Iterator interface.
 * The iterator interface allows client to traverse objects in a collection.
 *
 * @author Ivan Georgiev
 * @abstract
 */
class ig_Iterator {
	/**
	 * Checks whether the collection has more elements to traverse.
	 * If this method returns true, it is safe to use the {@link next} method
	 * to retrive the next element in collection.
	 * @return boolean Returns true if there are still elements to traverse.
	 * @abstract
	 */
	function hasNext() {
 
	}
 
	/**
	 * Retrieves and returns the next element in collection.
	 * Client should use the {@link hasNext} method to check if element is
	 * available before calling this method.
	 * @return Object
	 * @abstract
	 */
	function & next() {
 
	}
}

Known Implementations: Array Iterator in PHP

See also: PHP Collection Interface, PHP Array Collection, PHP Array Iterator

 
php/util/iterator.txt · Last modified: 2009/10/31 23:39 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki