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(): bool {
11        global $indent;
12        echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
13        return $this->pos<3;
14    }
15
16    public function next(): void {
17        global $indent;
18        echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
19        $this->myContent[$this->pos++];
20    }
21
22    public function rewind(): void {
23        global $indent;
24        echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
25        $this->pos=0;
26    }
27
28    public function current(): mixed {
29        global $indent;
30        echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
31        return $this->myContent[$this->pos];
32    }
33
34    public function key(): mixed {
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--EXPECTF--
69object(MealIterator)#%d (2) {
70  ["pos":"MealIterator":private]=>
71  int(0)
72  ["myContent":"MealIterator":private]=>
73  array(3) {
74    [0]=>
75    string(9) "breakfast"
76    [1]=>
77    string(5) "lunch"
78    [2]=>
79    string(6) "dinner"
80  }
81}
82-----( Simple iteration: )-----
83--> MealIterator::rewind (0)
84--> MealIterator::valid (0)
85--> MealIterator::current (0)
86--> MealIterator::key (0)
87meal 0 => breakfast
88--> MealIterator::next (0)
89--> MealIterator::valid (1)
90--> MealIterator::current (1)
91--> MealIterator::key (1)
92meal 1 => lunch
93--> MealIterator::next (1)
94--> MealIterator::valid (2)
95--> MealIterator::current (2)
96--> MealIterator::key (2)
97meal 2 => dinner
98--> MealIterator::next (2)
99--> MealIterator::valid (3)
100--> MealIterator::rewind (3)
101
102
103
104-----( Nested iteration: )-----
105 --> MealIterator::rewind (0)
106 --> MealIterator::valid (0)
107 --> MealIterator::current (0)
108 --> MealIterator::key (0)
109
110Top level 1:
111meal 0 => breakfast
112     --> MealIterator::rewind (0)
113     --> MealIterator::valid (0)
114     --> MealIterator::current (0)
115     --> MealIterator::key (0)
116     meal 0 => breakfast
117     --> MealIterator::next (0)
118     --> MealIterator::valid (1)
119     --> MealIterator::current (1)
120     --> MealIterator::key (1)
121     meal 1 => lunch
122     --> MealIterator::next (1)
123     --> MealIterator::valid (2)
124     --> MealIterator::current (2)
125     --> MealIterator::key (2)
126     meal 2 => dinner
127     --> MealIterator::next (2)
128     --> MealIterator::valid (3)
129 --> MealIterator::next (3)
130
131Warning: Undefined array key 3 in %s on line %d
132 --> MealIterator::valid (4)
133