xref: /php-src/tests/classes/iterators_004.phpt (revision 75a678a7)
1--TEST--
2ZE2 iterators must be implemented
3--FILE--
4<?php
5
6echo "1st try\n";
7
8class c1 {}
9
10$obj = new c1();
11
12foreach($obj as $w) {
13    echo "object:$w\n";
14}
15
16echo "2nd try\n";
17
18class c2 {
19
20    public $max = 3;
21    public $num = 0;
22
23    function current() {
24        echo __METHOD__ . "\n";
25        return $this->num;
26    }
27    function next(): void {
28        echo __METHOD__ . "\n";
29        $this->num++;
30    }
31    function valid(): bool {
32        echo __METHOD__ . "\n";
33        return $this->num < $this->max;
34    }
35    function key(): mixed {
36        echo __METHOD__ . "\n";
37        switch($this->num) {
38            case 0: return "1st";
39            case 1: return "2nd";
40            case 2: return "3rd";
41            default: return "???";
42        }
43    }
44}
45
46$obj = new c2();
47
48foreach($obj as $v => $w) {
49    echo "object:$v=>$w\n";
50}
51
52print "Done\n";
53?>
54--EXPECT--
551st try
562nd try
57object:max=>3
58object:num=>0
59Done
60