Predefined interface-Iterator

Source: Internet
Author: User
Tags rewind

<? 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...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.