Php/*An interface for an external iterator or class that can iterate internally. Iterator extends traversable {Abstract public mixed current (void) abstract public scalar key (void) abstract public void Next (void) abstract public void rewind (void) abstract public boolean valid (void)}*/classMyiteratorImplementsiterator{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; } functionvalid () {Var_dump(__method__ ); return isset($this-Array[$this-position]); }} $it=NewMyiterator;foreach($it as $key=$value ) { Print_r($key.' /'.$value ); Echo"
" ;}/*before the first execution of the Foreach loop, call Rewind, return to the first element of the iterator, start position can also control the second step of the execution valid check whether the current position is valid, effectively continue to execute, Invalid loop Stop third step execution current returns the value of the present element step fourth executes key to return the key of the current element fifth step: Execute next after the first loop to move forward to the next element to start the next cycle (Skip Rewind, Valid detection current position) down Next,valid,current,key loop execution, until valid to an invalid position*/
Content is their own summary, there is no place, I hope to point out!
More content to follow ...
http://www.bkjia.com/PHPjc/770158.html www.bkjia.com true http://www.bkjia.com/PHPjc/770158.html techarticle PHP/* Can internally iterate over the interface of its own external iterator or class. Iterator extends traversable {Abstract public mixed current (void) abstract public scalar key (void) abstract ...