Home  >  Article  >  Backend Development  >  Iterator usage example of PHP predefined interface

Iterator usage example of PHP predefined interface

coldplay.xixi
coldplay.xixiforward
2020-07-14 17:25:381857browse

Iterator usage example of PHP predefined interface

The example in this article describes the usage of Iterator of PHP predefined interface. Share it with everyone for your reference, as follows:

Iterator interface

An interface that can iterate internally through its own external iterator or class.

Interface summary

Iterator extends Traversable {
    /* 方法 */
    abstract public current ( void ) : mixed
    abstract public key ( void ) : scalar
    abstract public next ( void ) : void
    abstract public rewind ( void ) : void
    abstract public valid ( void ) : bool
}

Example:

<?php
class myIterator implements Iterator
{
  private $position = 0;
  private $array = array(
    &#39;first_element&#39;,
    &#39;second_element&#39;,
    &#39;last_element&#39;,
  );

  /**
   * 重置键的位置
   */
  public function rewind(): void
  {
    var_dump(__METHOD__);
    $this->position = 0;
  }

  /**
   * 返回当前元素
   */
  public function current()
  {
    var_dump(__METHOD__);
    return $this->array[$this->position];
  }

  /**
   * 返回当前元素的键
   * @return int
   */
  public function key(): int
  {
    var_dump(__METHOD__);
    return $this->position;
  }

  /**
   * 将键移动到下一位
   */
  public function next(): void
  {
    var_dump(__METHOD__);
    ++$this->position;
  }

  /**
   * 判断键所在位置的元素是否存在
   * @return bool
   */
  public function valid(): bool
  {
    var_dump(__METHOD__);
    return isset($this->array[$this->position]);
  }
}

$it = new myIterator;

foreach ($it as $key => $value) {
  var_dump($key, $value);
  echo "\n";
}

Output result:

string 'myIterator::rewind' (length=18 )
string 'myIterator::valid' (length=17)
string 'myIterator::current' (length=19)
string 'myIterator::key' (length=15)
int 0
string 'first_element' (length=13)
string 'myIterator::next' (length=16)
string 'myIterator::valid' (length=17)
string 'myIterator: :current' (length=19)
string 'myIterator::key' (length=15)
int 1
string 'second_element' (length=14)
string 'myIterator::next' (length=16)
string 'myIterator::valid' (length=17)
string 'myIterator::current' (length=19)
string 'myIterator::key' (length=15)
int 2
string 'last_element' (length=12)
string 'myIterator::next' (length=16)
string 'myIterator::valid' (length=17)

It can be seen from the results that when a class implements the Iterator interface and changes the class instance data set, the keys of the data set will first be reset, and then gradually moved back. Each time, the current element and the current key.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of Iterator usage example of PHP predefined interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete