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