Design Pattern in php: Iterator pattern
Myiterator = $ iterator; $ this-> position = 0;} // Obtain the current function current () {return $ this-> myiterator [$ this-> position];} // next indexfunction next () {++ $ this-> position;} // Obtain the key (subscript) function key () {return $ this-> position ;} function value () {return $ this-> myiterator [$ this-> position];} // re-traverse function rewind () {$ this-> position = 0 ;} function valid () {return isset ($ this-> myiterator [$ this-> position]) ;}// application/* $ aData = array ('A ', 'B', 'C'); $ myiterator = new myiterator ($ aData); while ($ myiterator-> valid () {$ myiterator-> next ();} * // (2) build your own container interface container {public function getIterator ();} class myContainer implements container {private $ iterator; public function _ construct ($ data) {$ this-> iterator = new myiterator ($ data);} public function getIterator () {return $ this-> iterator ;}} // application instance $ data = array ('A', 'B', 'C'); $ myContainer = new myContainer ($ data ); // get iterator $ myiterator = $ myContainer-> getIterator (); while ($ myiterator-> valid () {var_dump ($ myiterator-> current ()); $ myiterator-> next ();}