xref: /PHP-7.2/ext/spl/tests/spl_004.phpt (revision 1dab96c1)
1--TEST--
2SPL: iterator_apply()
3--FILE--
4<?php
5
6function my_error_handler($errno, $errstr, $errfile, $errline) {
7	echo "Error: $errstr\n";
8}
9
10set_error_handler('my_error_handler');
11
12function test_arg($arg)
13{
14	if ($arg instanceof Iterator)
15	{
16		var_dump($arg->key());
17		var_dump($arg->current());
18	}
19	else
20	{
21		var_dump($arg);
22	}
23	return true;
24}
25
26function test()
27{
28	static $arg = 0;
29	var_dump($arg++);
30	return true;
31}
32
33$it = new RecursiveArrayIterator(array(1, array(21, 22), 3));
34
35var_dump(iterator_apply($it, 'test', NULL));
36
37echo "===ARGS===\n";
38var_dump(iterator_apply($it, 'test_arg', array($it)));
39
40echo "===RECURSIVE===\n";
41$it = new RecursiveIteratorIterator($it);
42var_dump(iterator_apply($it, 'test'));
43
44echo "===ERRORS===\n";
45try {
46	var_dump(iterator_apply($it, 'test', 1));
47} catch (Error $e) {
48	my_error_handler($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
49}
50var_dump(iterator_apply($it, 'non_existing_function'));
51var_dump(iterator_apply($it, 'non_existing_function', NULL, 2));
52
53?>
54===DONE===
55<?php exit(0); ?>
56--EXPECT--
57int(0)
58int(1)
59int(2)
60int(3)
61===ARGS===
62int(0)
63int(1)
64int(1)
65array(2) {
66  [0]=>
67  int(21)
68  [1]=>
69  int(22)
70}
71int(2)
72int(3)
73int(3)
74===RECURSIVE===
75int(3)
76int(4)
77int(5)
78int(6)
79int(4)
80===ERRORS===
81Error: Argument 3 passed to iterator_apply() must be of the type array or null, integer given
82Error: iterator_apply() expects parameter 2 to be a valid callback, function 'non_existing_function' not found or invalid function name
83NULL
84Error: iterator_apply() expects at most 3 parameters, 4 given
85NULL
86===DONE===
87