An interface for an external iterator or class that can iterate internally.
Interface Summary
Iterator extends Traversable {/ * method */ Abstract public mixed current (void) abstract public scalar key (VO ID) abstract public void Next (void), abstract public void rewind (void), abstract Public, Boolean valid (V OID)}
Pre-defined Iterators
PHP has provided some iterators for everyday tasks, such as SPL iterators.
Example
Example #1 Basic Usage
This example shows the sequence of calls to the Iterator method when using foreach.
<?php class Myiterator implements Iterator {private $position = 0; Private $array = Array ("Firstelement", "Secondelement", "lastelement",); Public Function __construct () {$this->position = 0; } function Rewind () {var_dump (__method__); $this->position = 0; } function current () {var_dump (__method__); return $this->array[$this->position]; } function key () {var_dump (__method__); return $this->position; } function Next () {var_dump (__method__); + + $this->position; } function valid () {var_dump (__method__); return Isset ($this->array[$this->position]); }} $it = new Myiterator; foreach ($it as $key = + $value) {var_dump ($key, $value); echo "\ n"; }?>
The output of the above routines is similar to the following:
String "Myiterator::rewind" string (myiterator::valid) "Myiterator::current" string (15) " Myiterator::key "Int (0) string ()" Firstelement "string (+)" Myiterator::next "string (" Myiterator::valid "string ( "Myiterator::current" string ("Myiterator::key" int (1) "Secondelement" string () "Myiterator::next" String (2) "Myiterator::valid" string (11) "Myiterator::current" string ("Myiterator::key" Lastelement "string (myiterator::next)" string "Myiterator::valid"
Method List
iterator::current-returns the current element
iterator::key-returns the key of the current element
Iterator::next-move forward to the next element
Iterator::rewind-returns to the first element of an iterator
iterator::valid-Check if the current location is valid