xref: /PHP-8.1/tests/classes/iterators_006.phpt (revision 75a678a7)
1--TEST--
2ZE2 iterators and array wrapping
3--FILE--
4<?php
5
6class ai implements Iterator {
7
8    private $array;
9
10    function __construct() {
11        $this->array = array('foo', 'bar', 'baz');
12    }
13
14    function rewind(): void {
15        reset($this->array);
16        $this->next();
17    }
18
19    function valid(): bool {
20        return $this->key !== NULL;
21    }
22
23    function key(): mixed {
24        return $this->key;
25    }
26
27    function current(): mixed {
28        return $this->current;
29    }
30
31    function next(): void {
32        $this->key = key($this->array);
33        $this->current = current($this->array);
34        next($this->array);
35    }
36}
37
38class a implements IteratorAggregate {
39
40    public function getIterator(): Traversable {
41        return new ai();
42    }
43}
44
45$array = new a();
46
47foreach ($array as $property => $value) {
48    print "$property: $value\n";
49}
50
51#$array = $array->getIterator();
52#$array->rewind();
53#$array->valid();
54#var_dump($array->key());
55#var_dump($array->current());
56echo "===2nd===\n";
57
58$array = new ai();
59
60foreach ($array as $property => $value) {
61    print "$property: $value\n";
62}
63
64echo "===3rd===\n";
65
66foreach ($array as $property => $value) {
67    print "$property: $value\n";
68}
69
70?>
71--EXPECT--
720: foo
731: bar
742: baz
75===2nd===
760: foo
771: bar
782: baz
79===3rd===
800: foo
811: bar
822: baz
83