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