Home  >  Article  >  Backend Development  >  PHP iterators and implementation of iteration

PHP iterators and implementation of iteration

不言
不言Original
2018-04-19 15:43:152428browse

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 properties, just like array mode, 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 a 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
*/

##Related recommendations:

[php predefined interface] Iterator

Recursive algorithm implementation and iterative algorithm implementation of PHP quick sorting problem

PHP’s iterator interface How to use Iterator

The above is the detailed content of PHP iterators and implementation of iteration. 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
Previous article:php finallyNext article:php finally