Home  >  Article  >  Backend Development  >  PHP iterator and iteration implementation and usage analysis PHP skills

PHP iterator and iteration implementation and usage analysis PHP skills

jacklove
jackloveOriginal
2018-06-27 17:38:341853browse

This article mainly introduces PHP iterators and the implementation and use of iteration. It analyzes the concept, principle, definition and use of PHP iterators in the form of examples. Friends in need can refer to it

The examples in this article describe the implementation and use of PHP iterators and iterations. Share it with everyone for your reference, the details are as follows:

PHP’s object-oriented engine provides a very smart feature, that is, you can use the foreach() method to take out an object through a loop All attributes, just like the array method, the code is as follows:

class Myclass{
  public $a = 'php';
  public $b = 'onethink';
  public $c = 'thinkphp';
}
$myclass = new Myclass();
//用foreach()将对象的属性循环出来
foreach($myclass as $key.'=>'.$val){
  echo &#39;$&#39;.$key.&#39; = &#39;.$val."<br/>";
}
/*返回
  $a = php
  $b = onethink
  $c = thinkphp
*/

If you need to implement more complex behavior, you can pass an iterator( Iterator) to implement

//迭代器接口
interface MyIterator{
  //函数将内部指针设置回数据开始处
  function rewind();
  //函数将判断数据指针的当前位置是否还存在更多数据
  function valid();
  //函数将返回数据指针的值
  function key();
  //函数将返回将返回当前数据指针的值
  function value();
  //函数在数据中移动数据指针的位置
  function next();
}
//迭代器类
class ObjectIterator implements MyIterator{
  private $obj;//对象
  private $count;//数据元素的数量
  private $current;//当前指针
  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{
  //获取迭代器
  function getIterator();
}
class MyObject implements MyAggregate{
  public $data = array();
  function __construct($in){
    $this->data = $in;
  }
  function getIterator(){
    return new ObjectIterator($this);
  }
}
//迭代器的用法
$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.&#39;=>&#39;.$value;
  echo "<br/>";
}
/*返回
  0=>2
  1=>4
  2=>6
  3=>8
  4=>10
*/

Articles you may be interested in:

PHP techniques for operating MongoDB to implement the add, delete, modify, and query functions

Common PHP tips for operating Redis Summary of PHP tips

Analysis of PHP tips by using native PHP to read and write excel files




The above is the detailed content of PHP iterator and iteration implementation and usage analysis PHP skills. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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