"PHP Predefined Interface" iterator

Source: Internet
Author: User
Tags rewind scalar

This article is to share the content of "PHP predefined Interface" Iterator, now share to everyone, the need for friends can refer to the content of this article

Iterator (iterator) interface

Summary: An interface for an external iterator or class that can iterate internally.

Specific function explanation

Interface Summary:

Iterator extends Traversable {/    * method */    Abstract public mixed current (void) abstract public    scalar key (VO  ID) abstract public    void Next (void),    abstract public void rewind (void),    abstract public bool Valid (void )}

Example:

Example #1 Basic usage (the order in which iterator methods are called when using foreach) <?phpclass 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) {var_dump ($key, $value); echo "\ n";} 1.rewind, valid-------valid, Key3.next, valid, current, key 2.next, Gt Key4.Next-valid?> 

Iterator (iterator) interface

Summary: An interface for an external iterator or class that can iterate internally.

Specific function explanation

Interface Summary:

Iterator extends Traversable {/    * method */    Abstract public mixed current (void) abstract public    scalar key (VO  ID) abstract public    void Next (void),    abstract public void rewind (void),    abstract public bool Valid (void )}

Example:

Example #1 Basic usage (the order in which iterator methods are called when using foreach) <?phpclass 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) {var_dump ($key, $value); echo "\ n";} 1.rewind, valid-------valid, Key3.next, valid, current, key 2.next, Gt Key4.Next-valid?> 
Related Article

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.