This article is to share the content of "PHP predefined Interface" Iterator, now share to everyone, the need for friends can refer to the content of this article
Iterator (iterator) interface
Summary: An interface for an external iterator or class that can iterate internally.
Specific function explanation
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 bool Valid (void )}
Example:
Example #1 Basic usage (the order in which iterator methods are called when using foreach) <?phpclass 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";} 1.rewind, valid-------valid, Key3.next, valid, current, key 2.next, Gt Key4.Next-valid?>
Iterator (iterator) interface
Summary: An interface for an external iterator or class that can iterate internally.
Specific function explanation
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 bool Valid (void )}
Example:
Example #1 Basic usage (the order in which iterator methods are called when using foreach) <?phpclass 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";} 1.rewind, valid-------valid, Key3.next, valid, current, key 2.next, Gt Key4.Next-valid?>