summaryrefslogtreecommitdiff
path: root/Zend/tests/foreach_004.phpt
blob: 8a7b915c2e9693f7e639ac9d1f47fd6488a13b86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--TEST--
Iterator exceptions in foreach by reference
--FILE--
<?php
class IT extends ArrayIterator {
    private $n = 0;

    function __construct($trap = null) {
        parent::__construct([0, 1]);
        $this->trap = $trap;
    }

    function trap($trap) {
        if ($trap === $this->trap) {
            throw new Exception($trap);
        }
    }

    function rewind()  {$this->trap(__FUNCTION__); return parent::rewind();}
    function valid()   {$this->trap(__FUNCTION__); return parent::valid();}
    function key()     {$this->trap(__FUNCTION__); return parent::key();}
    function next()    {$this->trap(__FUNCTION__); return parent::next();}
}

foreach(['rewind', 'valid', 'key', 'next'] as $trap) {
    $obj = new IT($trap);
    try {
        // IS_CV
        foreach ($obj as $key => &$val) echo "$val\n";
    } catch (Exception $e) {
        echo $e->getMessage() . "\n";
    }
    unset($obj);

    try {
        // IS_VAR
        foreach (new IT($trap) as $key => &$val) echo "$val\n";
    } catch (Exception $e) {
        echo $e->getMessage() . "\n";
    }

    try {
        // IS_TMP_VAR
        foreach ((object)new IT($trap) as $key => &$val) echo "$val\n";
    } catch (Exception $e) {
        echo $e->getMessage() . "\n";
    }
}
?>
--EXPECT--
rewind
rewind
rewind
valid
valid
valid
key
key
key
0
next
0
next
0
next