最简单的迭代器:
1 <?php 2 3 class order{ 4 //无set get 暂不分装结构体; 5 public $a; 6 public $b; 7 public $c; 8 } 9 10 /** 11 * Iterator模式的简单实现类 12 */ 13 class sample implements Iterator { 14 private $_items ; 15 16 public function __construct(){ 17 for($i=0;$i<3;$i++){ 18 $a=new order(); 19 $a->a=$i*3; 20 $a->b=$i*4; 21 $a->c=$i*5; 22 $this->_items[]=$a; 23 } 24 } 25 26 27 28 // public function __construct(&$data) { 29 // $this->_items = $data; 30 // } 31 public function current() { 32 return current($this->_items); 33 } 34 35 public function next() { 36 next($this->_items); 37 } 38 39 public function key() { 40 return key($this->_items); 41 } 42 43 public function rewind() { 44 reset($this->_items); 45 } 46 47 public function valid() { 48 return ($this->current() !== FALSE); 49 } 50 } 51 52 /** DEMO */ 53 $sa = new sample(); 54 print_r($sa); 55 foreach ($sa AS $key => $row) { 56 print_r($row->a); 57 } 58 59 ?>