PHP Iterator and Iteration Implementation and Application Analysis

  • 2021-09-16 06:34:15
  • OfStack

This paper describes the implementation and use of PHP iterator and iteration with examples. Share it for your reference, as follows:

The object-oriented engine of PHP provides a very clever feature, that is, you can use the foreach() Method takes out all the properties of an object in a loop, just like Array Mode 1. The code is as follows:


class Myclass{
  public $a = 'php';
  public $b = 'onethink';
  public $c = 'thinkphp';
}
$myclass = new Myclass();
// Use foreach() Loop out the properties of an object 
foreach($myclass as $key.'=>'.$val){
  echo '$'.$key.' = '.$val."<br/>";
}
/* Return 
  $a = php
  $b = onethink
  $c = thinkphp
*/

If you need to implement more complex behaviors, you can use 1 iterator (Iterator) to implement the


// Iterator interface 
interface MyIterator{
  // Function sets the internal pointer back to the beginning of the data 
  function rewind();
  // Function will determine whether there is more data at the current position of the data pointer 
  function valid();
  // The function returns the value of the data pointer 
  function key();
  // The function returns the value that will return the current data pointer 
  function value();
  // Function moves the position of the data pointer in the data 
  function next();
}
// Iterator class 
class ObjectIterator implements MyIterator{
  private $obj;// Object 
  private $count;// Number of data elements 
  private $current;// Current pointer 
  function __construct($obj){
    $this->obj = $obj;
    $this->count = count($this->obj->data);
  }
  function rewind(){
    $this->current = 0;
  }
  function valid(){
    return $this->current < $this->count;
  }
  function key(){
    return $this->current;
  }
  function value(){
    return $this->obj->data[$this->current];
  }
  function next(){
    $this->current++;
  }
}
interface MyAggregate{
  // Get iterator 
  function getIterator();
}
class MyObject implements MyAggregate{
  public $data = array();
  function __construct($in){
    $this->data = $in;
  }
  function getIterator(){
    return new ObjectIterator($this);
  }
}
// Usage of iterators 
$arr = array(2,4,6,8,10);
$myobject = new MyObject($arr);
$myiterator = $myobject->getIterator();
for($myiterator->rewind();$myiterator->valid();$myiterator->next()){
  $key = $myiterator->key();
  $value = $myiterator->value();
  echo $key.'=>'.$value;
  echo "<br/>";
}
/* Return 
  0=>2
  1=>4
  2=>6
  3=>8
  4=>10
*/

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 Grammar", "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: