PHP iterator pattern explanation of PHP design pattern

Time:2020-6-27

Iterators, sometimes called cursors, are software design patterns for programming. They are interfaces that can be accessed on container objects (such as lists or vectors). Designers do not need to care about the contents of container objects.

There are different ways to implement iterator in various languages. Some object-oriented languages like Java, C, Python and Delphi have built-in features of iterator, which are perfectly integrated with the language. We call it implicit iterator, but like C + + language itself, it has no features of iterator, but STL still implements a powerful iterator using template.

PHP 5 started to support interfaces and built-in iterator interface, so if you define a class and implement the iterator interface, your class object isZEND_ITER_OBJECT, otherwiseZEND_ITER_PLAIN_OBJECT.

aboutZEND_ITER_PLAIN_OBJECTForeach will passHASH_OFGet the default property array of the object, and foreach the array

First, let’s look at the definition of iterator, which is to provide a method to access the elements of an aggregate object in sequence without exposing the internal display of the object. It can help construct specific objects that can provide a single standard interface loop or iterate over any type of countable data. Let’s look at the structure diagram of the iterator pattern:

Let’s look at the internal methods that iterators need:

  • Iterator:: current – return the current element
  • Iterator:: key – return the key of the current element
  • Iterator:: next – move forward to next element
  • Iterator:: rewind – rewind the iterator to the first element
  • Iterator:: valid – checks if current position is valid

And for Zend_ ITER_ The class object of object will be foreach by calling the related functions of iterator interface implemented by the object.

<?php
/**
*Simple implementation class of iterator pattern
*/
class sample implements Iterator {
  private $_items ;
  public function __construct(&$data) {
    $this->_items = $data;
  }
  public function current() {
    return current($this->_items);
  }
  public function next() {
    next($this->_items);  
  }
  public function key() {
    return key($this->_items);
  }
  public function rewind() {
    reset($this->_items);
  }
  public function valid() {                                       
    return ($this->current() !== FALSE);
  }
}
/** DEMO */
$data = array(1, 2, 3, 4, 5);
$sa = new sample($data);
foreach ($sa AS $key => $row) {
  echo $key, ' ', $row, '<br />';
}
?>

Here are a few examples of the scope of use of iterators:

When using packages or libraries that return iterators (such as SPL iterators in PHP5)
Cannot get all elements of the container in one call
To process a large number of prime free times (data in GB for tables in the database)
……

Different iterators have different interfaces, for example, PHP SPL iterators include next () (move to the next element), core () (return the current element), valid () (check the end of the iteration), rewind () (restart from the beginning), key () (return the index of the current element). Of course, you can write your own iterator, or you can use the iterator in the system.

In general, we use foreach to use iterators. Here is the code:


<?php  
class sample implements Iterator
{
  private $_items = array(1,2,3,4,5,6,7);
  public function __construct() {
         ;//void
  }
  public function rewind() { reset($this->_items); }
  public function current() { return current($this->_items); }
  public function key() { return key($this->_items); }
  public function next() { return next($this->_items); }
  public function valid() { return ( $this->current() !== false ); }
}
$sa = new sample();
foreach($sa as $key => $val){
  print $key . "=>" .$val;
}
?> 

While loops can also:

<?php
While ($itertor - > valid()) {// judge whether it is the last element
 $element = $itertor - > current(); // get the current element
 $itertor - > next(); // move to next element
}
?>

Why learn about PHP iterators? There is a very important reason: PHP’s iterator can be used to implement common data structures, such as lists, stacks, queues and graphs. Later, we will do a special topic, using PHP to implement most of the data structures, and in the form of object-oriented. So I’ve warmed up the PHP iterator.

summary

The above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. Thank you for your support for developepaer. If you want to know more about it, please check the relevant links below