summaryrefslogtreecommitdiff
path: root/ext/spl/tests/spl_004.phpt
blob: c31d0b6157dc41f42dad8b3c64a1c18f9d94c8f0 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
--TEST--
SPL: iterator_apply()
--FILE--
<?php

function my_error_handler($errno, $errstr, $errfile, $errline) {
    echo "Error: $errstr\n";
}

set_error_handler('my_error_handler');

function test_arg($arg)
{
    if ($arg instanceof Iterator)
    {
        var_dump($arg->key());
        var_dump($arg->current());
    }
    else
    {
        var_dump($arg);
    }
    return true;
}

function test()
{
    static $arg = 0;
    var_dump($arg++);
    return true;
}

$it = new RecursiveArrayIterator(array(1, array(21, 22), 3));

var_dump(iterator_apply($it, 'test', NULL));

echo "===ARGS===\n";
var_dump(iterator_apply($it, 'test_arg', array($it)));

echo "===RECURSIVE===\n";
$it = new RecursiveIteratorIterator($it);
var_dump(iterator_apply($it, 'test'));

echo "===ERRORS===\n";
try {
    var_dump(iterator_apply($it, 'test', 1));
} catch (TypeError $e) {
    echo $e->getMessage(), "\n";
}
try {
    var_dump(iterator_apply($it, 'non_existing_function'));
} catch (TypeError $e) {
    echo $e->getMessage(), "\n";
}
try {
    var_dump(iterator_apply($it, 'non_existing_function', NULL, 2));
} catch (TypeError $e) {
    echo $e->getMessage(), "\n";
}

?>
--EXPECT--
int(0)
int(1)
int(2)
int(3)
===ARGS===
int(0)
int(1)
int(1)
array(2) {
  [0]=>
  int(21)
  [1]=>
  int(22)
}
int(2)
int(3)
int(3)
===RECURSIVE===
int(3)
int(4)
int(5)
int(6)
int(4)
===ERRORS===
iterator_apply(): Argument #3 ($args) must be of type ?array, int given
iterator_apply(): Argument #2 ($function) must be a valid callback, function "non_existing_function" not found or invalid function name
iterator_apply() expects at most 3 parameters, 4 given