1--TEST-- 2foreach with Iterator. 3--FILE-- 4<?php 5 6class MealIterator implements Iterator { 7 private $pos=0; 8 private $myContent=array("breakfast", "lunch", "dinner"); 9 10 public function valid() { 11 global $indent; 12 echo "$indent--> " . __METHOD__ . " ($this->pos)\n"; 13 return $this->pos<3; 14 } 15 16 public function next() { 17 global $indent; 18 echo "$indent--> " . __METHOD__ . " ($this->pos)\n"; 19 return $this->myContent[$this->pos++]; 20 } 21 22 public function rewind() { 23 global $indent; 24 echo "$indent--> " . __METHOD__ . " ($this->pos)\n"; 25 $this->pos=0; 26 } 27 28 public function current() { 29 global $indent; 30 echo "$indent--> " . __METHOD__ . " ($this->pos)\n"; 31 return $this->myContent[$this->pos]; 32 } 33 34 public function key() { 35 global $indent; 36 echo "$indent--> " . __METHOD__ . " ($this->pos)\n"; 37 return "meal " . $this->pos; 38 } 39 40} 41 42$f = new MealIterator; 43var_dump($f); 44 45echo "-----( Simple iteration: )-----\n"; 46foreach ($f as $k=>$v) { 47 echo "$k => $v\n"; 48} 49 50$f->rewind(); 51 52$indent = " "; 53 54echo "\n\n\n-----( Nested iteration: )-----\n"; 55$count=1; 56foreach ($f as $k=>$v) { 57 echo "\nTop level " . $count++ . ": \n"; 58 echo "$k => $v\n"; 59 $indent = " "; 60 foreach ($f as $k=>$v) { 61 echo " $k => $v\n"; 62 } 63 $indent = " "; 64 65} 66 67?> 68===DONE=== 69--EXPECTF-- 70object(MealIterator)#%d (2) { 71 ["pos":"MealIterator":private]=> 72 int(0) 73 ["myContent":"MealIterator":private]=> 74 array(3) { 75 [0]=> 76 string(9) "breakfast" 77 [1]=> 78 string(5) "lunch" 79 [2]=> 80 string(6) "dinner" 81 } 82} 83-----( Simple iteration: )----- 84--> MealIterator::rewind (0) 85--> MealIterator::valid (0) 86--> MealIterator::current (0) 87--> MealIterator::key (0) 88meal 0 => breakfast 89--> MealIterator::next (0) 90--> MealIterator::valid (1) 91--> MealIterator::current (1) 92--> MealIterator::key (1) 93meal 1 => lunch 94--> MealIterator::next (1) 95--> MealIterator::valid (2) 96--> MealIterator::current (2) 97--> MealIterator::key (2) 98meal 2 => dinner 99--> MealIterator::next (2) 100--> MealIterator::valid (3) 101--> MealIterator::rewind (3) 102 103 104 105-----( Nested iteration: )----- 106 --> MealIterator::rewind (0) 107 --> MealIterator::valid (0) 108 --> MealIterator::current (0) 109 --> MealIterator::key (0) 110 111Top level 1: 112meal 0 => breakfast 113 --> MealIterator::rewind (0) 114 --> MealIterator::valid (0) 115 --> MealIterator::current (0) 116 --> MealIterator::key (0) 117 meal 0 => breakfast 118 --> MealIterator::next (0) 119 --> MealIterator::valid (1) 120 --> MealIterator::current (1) 121 --> MealIterator::key (1) 122 meal 1 => lunch 123 --> MealIterator::next (1) 124 --> MealIterator::valid (2) 125 --> MealIterator::current (2) 126 --> MealIterator::key (2) 127 meal 2 => dinner 128 --> MealIterator::next (2) 129 --> MealIterator::valid (3) 130 --> MealIterator::next (3) 131 132Notice: Undefined offset: 3 in %s on line %d 133 --> MealIterator::valid (4) 134===DONE=== 135