xref: /PHP-5.6/tests/classes/iterators_007.phpt (revision 610c7fbe)
1--TEST--
2ZE2 iterators and exceptions
3--FILE--
4<?php
5class Test implements Iterator
6{
7	public $arr = array(1, 2, 3);
8	public $x = 0;
9
10	public function rewind()    { if ($this->x == 0) throw new Exception(__METHOD__); reset($this->arr); }
11	public function current()   { if ($this->x == 1) throw new Exception(__METHOD__); return current($this->arr); }
12	public function key()       { if ($this->x == 2) throw new Exception(__METHOD__); return key($this->arr); }
13	public function next()      { if ($this->x == 3) throw new Exception(__METHOD__); next($this->arr); }
14	public function valid()     { if ($this->x == 4) throw new Exception(__METHOD__); return (key($this->arr) !== NULL); }
15}
16
17$t = new Test();
18
19while($t->x < 5)
20{
21	try
22	{
23	    foreach($t as $k => $v)
24	    {
25	        echo "Current\n";
26	    }
27	}
28	catch(Exception $e)
29	{
30	    echo "Caught in " . $e->getMessage() . "()\n";
31	}
32	$t->x++;
33}
34?>
35===DONE===
36--EXPECT--
37Caught in Test::rewind()
38Caught in Test::current()
39Caught in Test::key()
40Current
41Caught in Test::next()
42Caught in Test::valid()
43===DONE===
44