Usage Analysis of PHP Iterator Interface Iterator

  • 2021-08-31 07:28:01
  • OfStack

This article illustrates the use of PHP iterator interface Iterator. Share it for your reference, as follows:

The PHP Iterator interface allows an object to iterate through internal data in its own way so that it can be iterated through. The Iterator interface is summarized as follows:


Iterator extends Traversable {
  // Returns the element pointed to by the current index cursor 
  abstract public mixed current ( void )
  // Returns the key pointed to by the current index cursor 
  abstract public scalar key ( void )
  // Move the current index cursor below 1 Element 
  abstract public void next ( void )
  // Reset index cursor 
  abstract public void rewind ( void )
  // Determines whether the element pointed to by the current index cursor is valid 
  abstract public boolean valid ( void )
}

Here is a simple example of how to use Iterator:


<?php
/**
 *  This class allows external iteration of its own internal private properties $_test And demonstrates the iterative process 
 *
 * @author  Crazy old driver 
 */
class TestIterator implements Iterator {
  /*
   *  Define the array to iterate over 
   */
  private $_test = array('dog', 'cat', 'pig');
  /*
   *  Indexed cursor 
   */
  private $_key = 0;
  /*
   *  Execute steps 
   */
  private $_step = 0;
  /**
   *  Point the index cursor to the initial position 
   *
   * @see TestIterator::rewind()
   */
  public function rewind() {
    echo ' No. 1 '.++$this->_step.' Step: Execute  '.__METHOD__.'<br>';
    $this->_key = 0;
  }
  /**
   *  Determines whether the element pointed to by the current index cursor is set 
   *
   * @see TestIterator::valid()
   * @return bool
   */
  public function valid() {
    echo ' No. 1 '.++$this->_step.' Step: Execute  '.__METHOD__.'<br>';
    return isset($this->_test[$this->_key]);
  }
  /**
   *  Points the current index down 1 Position 
   *
   * @see TestIterator::next()
   */
  public function next() {
    echo ' No. 1 '.++$this->_step.' Step: Execute  '.__METHOD__.'<br>';
    $this->_key++;
  }
  /**
   *  Returns the value of the element pointed to by the current index cursor 
   *
   * @see TestIterator::current()
   * @return value
   */
  public function current() {
    echo ' No. 1 '.++$this->_step.' Step: Execute  '.__METHOD__.'<br>';
    return $this->_test[$this->_key];
  }
  /**
   *  Returns the current index value 
   *
   * @return key
   * @see TestIterator::key()
   */
  public function key() {
    echo ' No. 1 '.++$this->_step.' Step: Execute  '.__METHOD__.'<br>';
    return $this->_key;
  }
}
$iterator = new TestIterator();
foreach($iterator as $key => $value){
  echo " The output index is {$key} Elements of ".":$value".'<br><br>';
}
?>

The above example will output:


 No. 1 1 Step: Execute  TestIterator::rewind
 No. 1 2 Step: Execute  TestIterator::valid
 No. 1 3 Step: Execute  TestIterator::current
 No. 1 4 Step: Execute  TestIterator::key
 The output index is 0 Elements of :dog
 No. 1 5 Step: Execute  TestIterator::next
 No. 1 6 Step: Execute  TestIterator::valid
 No. 1 7 Step: Execute  TestIterator::current
 No. 1 8 Step: Execute  TestIterator::key
 The output index is 1 Elements of :cat
 No. 1 9 Step: Execute  TestIterator::next
 No. 1 10 Step: Execute  TestIterator::valid
 No. 1 11 Step: Execute  TestIterator::current
 No. 1 12 Step: Execute  TestIterator::key
 The output index is 2 Elements of :pig
 No. 1 13 Step: Execute  TestIterator::next
 No. 1 14 Step: Execute  TestIterator::valid

As you can see from the above example, if valid returns false, the loop ends.

More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: