xref: /PHP-5.6/tests/classes/iterators_003.phpt (revision 610c7fbe)
1--TEST--
2ZE2 iterators and break
3--SKIPIF--
4<?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?>
5--FILE--
6<?php
7class c_iter implements Iterator {
8
9	private $obj;
10	private $num = 0;
11
12	function __construct($obj) {
13		echo __METHOD__ . "\n";
14		$this->obj = $obj;
15	}
16	function rewind() {
17		echo __METHOD__ . "\n";
18	}
19	function valid() {
20		$more = $this->num < $this->obj->max;
21		echo __METHOD__ . ' = ' .($more ? 'true' : 'false') . "\n";
22		return $more;
23	}
24	function current() {
25		echo __METHOD__ . "\n";
26		return $this->num;
27	}
28	function next() {
29		echo __METHOD__ . "\n";
30		$this->num++;
31	}
32	function key() {
33		return $this->num;
34	}
35}
36
37class c implements IteratorAggregate {
38
39	public $max = 4;
40
41	function getIterator() {
42		echo __METHOD__ . "\n";
43		return new c_iter($this);
44	}
45}
46
47$t = new c();
48
49foreach($t as $v) {
50	if ($v == 0) {
51		echo "continue outer\n";
52		continue;
53	}
54	foreach($t as $w) {
55		if ($w == 1) {
56			echo "continue inner\n";
57			continue;
58		}
59		if ($w == 2) {
60			echo "break inner\n";
61			break;
62		}
63		echo "double:$v:$w\n";
64	}
65	if ($v == 2) {
66		echo "break outer\n";
67		break;
68	}
69}
70
71print "Done\n";
72?>
73--EXPECT--
74c::getIterator
75c_iter::__construct
76c_iter::rewind
77c_iter::valid = true
78c_iter::current
79continue outer
80c_iter::next
81c_iter::valid = true
82c_iter::current
83c::getIterator
84c_iter::__construct
85c_iter::rewind
86c_iter::valid = true
87c_iter::current
88double:1:0
89c_iter::next
90c_iter::valid = true
91c_iter::current
92continue inner
93c_iter::next
94c_iter::valid = true
95c_iter::current
96break inner
97c_iter::next
98c_iter::valid = true
99c_iter::current
100c::getIterator
101c_iter::__construct
102c_iter::rewind
103c_iter::valid = true
104c_iter::current
105double:2:0
106c_iter::next
107c_iter::valid = true
108c_iter::current
109continue inner
110c_iter::next
111c_iter::valid = true
112c_iter::current
113break inner
114break outer
115Done
116