Pre-defined interface-iterator iterator_php tutorial

Source: Internet
Author: User
 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 ...

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