1--TEST-- 2ZE2 iterators and foreach 3--SKIPIF-- 4<?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?> 5--FILE-- 6<?php 7class c_iter implements Iterator { 8 9 private $obj; 10 private $num = 0; 11 12 function __construct($obj) { 13 echo __METHOD__ . "\n"; 14 $this->num = 0; 15 $this->obj = $obj; 16 } 17 function rewind() { 18 } 19 function valid() { 20 $more = $this->num < $this->obj->max; 21 echo __METHOD__ . ' = ' .($more ? 'true' : 'false') . "\n"; 22 return $more; 23 } 24 function current() { 25 echo __METHOD__ . "\n"; 26 return $this->num; 27 } 28 function next() { 29 echo __METHOD__ . "\n"; 30 $this->num++; 31 } 32 function key() { 33 echo __METHOD__ . "\n"; 34 switch($this->num) { 35 case 0: return "1st"; 36 case 1: return "2nd"; 37 case 2: return "3rd"; 38 default: return "???"; 39 } 40 } 41} 42 43class c implements IteratorAggregate { 44 45 public $max = 3; 46 47 function getIterator() { 48 echo __METHOD__ . "\n"; 49 return new c_iter($this); 50 } 51} 52 53echo "===Array===\n"; 54 55$a = array(0,1,2); 56foreach($a as $v) { 57 echo "array:$v\n"; 58} 59 60echo "===Manual===\n"; 61$t = new c(); 62for ($iter = $t->getIterator(); $iter->valid(); $iter->next()) { 63 echo $iter->current() . "\n"; 64} 65 66echo "===foreach/std===\n"; 67foreach($t as $v) { 68 echo "object:$v\n"; 69} 70 71echo "===foreach/rec===\n"; 72foreach($t as $v) { 73 foreach($t as $w) { 74 echo "double:$v:$w\n"; 75 } 76} 77 78echo "===foreach/key===\n"; 79foreach($t as $i => $v) { 80 echo "object:$i=>$v\n"; 81} 82 83print "Done\n"; 84exit(0); 85?> 86--EXPECT-- 87===Array=== 88array:0 89array:1 90array:2 91===Manual=== 92c::getIterator 93c_iter::__construct 94c_iter::valid = true 95c_iter::current 960 97c_iter::next 98c_iter::valid = true 99c_iter::current 1001 101c_iter::next 102c_iter::valid = true 103c_iter::current 1042 105c_iter::next 106c_iter::valid = false 107===foreach/std=== 108c::getIterator 109c_iter::__construct 110c_iter::valid = true 111c_iter::current 112object:0 113c_iter::next 114c_iter::valid = true 115c_iter::current 116object:1 117c_iter::next 118c_iter::valid = true 119c_iter::current 120object:2 121c_iter::next 122c_iter::valid = false 123===foreach/rec=== 124c::getIterator 125c_iter::__construct 126c_iter::valid = true 127c_iter::current 128c::getIterator 129c_iter::__construct 130c_iter::valid = true 131c_iter::current 132double:0:0 133c_iter::next 134c_iter::valid = true 135c_iter::current 136double:0:1 137c_iter::next 138c_iter::valid = true 139c_iter::current 140double:0:2 141c_iter::next 142c_iter::valid = false 143c_iter::next 144c_iter::valid = true 145c_iter::current 146c::getIterator 147c_iter::__construct 148c_iter::valid = true 149c_iter::current 150double:1:0 151c_iter::next 152c_iter::valid = true 153c_iter::current 154double:1:1 155c_iter::next 156c_iter::valid = true 157c_iter::current 158double:1:2 159c_iter::next 160c_iter::valid = false 161c_iter::next 162c_iter::valid = true 163c_iter::current 164c::getIterator 165c_iter::__construct 166c_iter::valid = true 167c_iter::current 168double:2:0 169c_iter::next 170c_iter::valid = true 171c_iter::current 172double:2:1 173c_iter::next 174c_iter::valid = true 175c_iter::current 176double:2:2 177c_iter::next 178c_iter::valid = false 179c_iter::next 180c_iter::valid = false 181===foreach/key=== 182c::getIterator 183c_iter::__construct 184c_iter::valid = true 185c_iter::current 186c_iter::key 187object:1st=>0 188c_iter::next 189c_iter::valid = true 190c_iter::current 191c_iter::key 192object:2nd=>1 193c_iter::next 194c_iter::valid = true 195c_iter::current 196c_iter::key 197object:3rd=>2 198c_iter::next 199c_iter::valid = false 200Done 201