xref: /PHP-7.2/tests/classes/iterators_006.phpt (revision 17ccbeec)
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() {
15		reset($this->array);
16		$this->next();
17	}
18
19	function valid() {
20		return $this->key !== NULL;
21	}
22
23	function key() {
24		return $this->key;
25	}
26
27	function current() {
28		return $this->current;
29	}
30
31	function next() {
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() {
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===DONE===
72--EXPECT--
730: foo
741: bar
752: baz
76===2nd===
770: foo
781: bar
792: baz
80===3rd===
810: foo
821: bar
832: baz
84===DONE===
85