<? Php/* Can internally iterate its own external iterator or class interface. 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 )} */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) {print_r ($ key. '/'. $ value); echo "<br/>";}/* before the first execution of the foreach loop, call rewind to return the first element of the iterator, the start position can also control the second step to execute valid to check whether the current position is valid and continue to execute effectively, if it is invalid, the loop stops. The third step executes current to return the value of the current element. The fourth step executes key to return the key of the current element. Step 5: after the first cycle ends, execute next to move forward to the next element to start the next cycle (then skip rewind and check the current location for valid). Execute next, valid, current, and key in sequence, until the valid is invalid */
The content is summarized by myself. If something is wrong, I hope to point it out!
More content will be added later...