Iterator usage example of PHP predefined interface

coldplay.xixi
Release: 2023-04-09 08:06:01
forward
1893 people have browsed it

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
}
Copy after login

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";
}
Copy after login

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!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!