Introduced
An interface for an external iterator or class that can iterate internally.
Iterator extends traversable{/* method */Abstract public mixed current (void) abstract public scalar key (void) AB Stract public void Next (void), abstract public void rewind (void), abstract public boolean valid (void)}
To give a chestnut, define an iterator:
class myierator implements iterator{ protected $position;//Hands public $arr = []; public function __ Construct (array $arr) { $info = date (' y-m-d h:i:s ') . ' ' . __METHOD__ . ' line : ' . __LINE__ . "\ r \ n"; $this->writelog ($ info); $this->arr = $arr; return $this; } //gets the current value public function current () { $info = date (' y-m-d h:i:s ') . ' ' . __METHOD__ . ' line : ' &nbsP;. __LINE__ . "\ r \ n"; $this->writelog ($info); return $this->arr[$this->position] ?? null; } //move the pointer to the next public Function next () { $info = date (' y-m-d h:i:s ') . ' ' . __METHOD__ . ' line : ' . __LINE__ . "\ r \ n"; $this->writelog ($ info); return ++ $this->position; } //returns the current pointer public function key () { $info = date (' y-m-d h:i:s ') . ' '  . __METHOD__ . ' line : ' . __LINE__ . ' \ r \ n '; $this->writelog ($info); return $this->position ; } //Reset Pointer public function Rewind () { $info = date (' y-m-d h:i:s ') . ' ' . __METHOD__ . ' line : ' . __line__ . "\ r \ n"; $this->writelog ($info); $this->position = 0; } //Check whether it is currently active public function valid () { $info = date (' y-m-d h:i:s ') . ' ' . __method__&nBSP;. ' line : ' . __LINE__ . ' \ r \ n '; $this->writelog ($info); return isset ($this, arr[$this->position]); } private function writelog ( string $info) { error_log ($info, 3, './debug.log '); }}
$zhangsan =[' One ', ' one ', ' one ', ' three ', ' four ']; $arr = new Myierator ($zhangsan); foreach ($arr as $k + $v) {echo "{$k} = {$v} \ r \ n ";}
Printing results:
0 = One
1 =
2 = Three
3 = Four
To view the call log:
2017-11-27 15:56:04 myierator::__construct line:16
2017-11-27 15:56:04 myierator::rewind line:50 Reset pointer
2017-11-27 15:56:04 myierator::valid line:58 Check is valid
2017-11-27 15:56:04 myierator::current line:25 Gets the value of the current pointer
2017-11-27 15:56:04 myierator::key line:41 get current pointer position
2017-11-27 15:56:04 myierator::next line:33 pointer moves down one (into the next loop)
2017-11-27 15:56:04 Myierator::valid line:58
2017-11-27 15:56:04 myierator::current line:25
2017-11-27 15:56:04 Myierator::key line:41
2017-11-27 15:56:04 Myierator::next line:33
2017-11-27 15:56:04 Myierator::valid line:58
2017-11-27 15:56:04 myierator::current line:25
2017-11-27 15:56:04 Myierator::key line:41
2017-11-27 15:56:04 Myierator::next line:33
2017-11-27 15:56:04 Myierator::valid line:58
2017-11-27 15:56:04 myierator::current line:25
2017-11-27 15:56:04 Myierator::key line:41
2017-11-27 15:56:04 Myierator::next line:33
2017-11-27 15:56:04 Myierator::valid line:58
//Traverse mode two:
$arr->rewind ();
while($arr->valid ()) {
$key= $arr->key ();
$value= $arr->current ();
Echo"{$key}={$value} \ r \ n";
$arr->next ();
}
PHP Iterator Iterator interface