2 class EnglishMealIterator
implements Iterator
{
4 private $myContent=array("breakfast", "dinner", "tea");
6 public function valid() {
8 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
9 return $this->pos
< count($this->myContent
);
12 public function next() {
14 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
18 public function rewind() {
20 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
24 public function current() {
26 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
27 return $this->myContent
[$this->pos
];
30 public function key() {
32 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
33 return "meal " . $this->pos
;
38 class FrenchMealIterator
implements Iterator
{
40 private $myContent=array("petit dejeuner", "dejeuner", "gouter", "dinner");
42 public function valid() {
44 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
45 return $this->pos
< count($this->myContent
);
48 public function next() {
50 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
54 public function rewind() {
56 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
60 public function current() {
62 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
63 return $this->myContent
[$this->pos
];
66 public function key() {
68 echo "$indent--> " . __METHOD__
. " ($this->pos)\n";
69 return "meal " . $this->pos
;
75 Class EuropeanMeals
implements IteratorAggregate
{
77 private $storedEnglishMealIterator;
78 private $storedFrenchMealIterator;
80 public function __construct() {
81 $this->storedEnglishMealIterator
= new EnglishMealIterator
;
82 $this->storedFrenchMealIterator
= new FrenchMealIterator
;
85 public function getIterator() {
87 echo "$indent--> " . __METHOD__
. "\n";
89 //Alternate between English and French meals
92 return $this->storedEnglishMealIterator
;
94 return $this->storedFrenchMealIterator
;
100 $f = new EuropeanMeals
;
103 echo "-----( Simple iteration 1: )-----\n";
104 foreach ($f as $k=>$v) {
107 echo "-----( Simple iteration 2: )-----\n";
108 foreach ($f as $k=>$v) {
114 echo "\n\n\n-----( Nested iteration: )-----\n";
116 foreach ($f as $k=>$v) {
117 echo "\nTop level " . $count++
. ": \n";
120 foreach ($f as $k=>$v) {