xref: /PHP-8.0/Zend/tests/002.phpt (revision 5770b667)
1--TEST--
2func_get_arg() tests
3--FILE--
4<?php
5
6function test1() {
7    try {
8        var_dump(func_get_arg(-10));
9    } catch (\ValueError $e) {
10        echo $e->getMessage() . \PHP_EOL;
11    }
12
13    try {
14        var_dump(func_get_arg(0));
15    } catch (\Error $e) {
16        echo $e->getMessage() . \PHP_EOL;
17    }
18    try {
19        var_dump(func_get_arg(1));
20    } catch (\Error $e) {
21        echo $e->getMessage() . \PHP_EOL;
22    }
23}
24
25function test2($a) {
26    try {
27        var_dump(func_get_arg(0));
28    } catch (\Error $e) {
29        echo $e->getMessage() . \PHP_EOL;
30    }
31    try {
32        var_dump(func_get_arg(1));
33    } catch (\Error $e) {
34        echo $e->getMessage() . \PHP_EOL;
35    }
36}
37
38function test3($a, $b) {
39    try {
40        var_dump(func_get_arg(0));
41    } catch (\Error $e) {
42        echo $e->getMessage() . \PHP_EOL;
43    }
44    try {
45        var_dump(func_get_arg(1));
46    } catch (\Error $e) {
47        echo $e->getMessage() . \PHP_EOL;
48    }
49    try {
50        var_dump(func_get_arg(2));
51    } catch (\Error $e) {
52        echo $e->getMessage() . \PHP_EOL;
53    }
54}
55
56test1();
57test1(10);
58test2(1);
59try {
60    test2();
61} catch (Throwable $e) {
62    echo "Exception: " . $e->getMessage() . "\n";
63}
64test3(1,2);
65
66call_user_func("test1");
67try {
68    call_user_func("test3", 1);
69} catch (Throwable $e) {
70    echo "Exception: " . $e->getMessage() . "\n";
71}
72call_user_func("test3", 1, 2);
73
74class test {
75    static function test1($a) {
76        try {
77            var_dump(func_get_arg(0));
78        } catch (\Error $e) {
79            echo $e->getMessage() . \PHP_EOL;
80        }
81        try {
82            var_dump(func_get_arg(1));
83        } catch (\Error $e) {
84            echo $e->getMessage() . \PHP_EOL;
85        }
86    }
87}
88
89test::test1(1);
90try {
91    var_dump(func_get_arg(1));
92} catch (\Error $e) {
93    echo $e->getMessage() . \PHP_EOL;
94}
95
96echo "Done\n";
97?>
98--EXPECTF--
99func_get_arg(): Argument #1 ($position) must be greater than or equal to 0
100func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
101func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
102func_get_arg(): Argument #1 ($position) must be greater than or equal to 0
103int(10)
104func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
105int(1)
106func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
107Exception: Too few arguments to function test2(), 0 passed in %s002.php on line %d and exactly 1 expected
108int(1)
109int(2)
110func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
111func_get_arg(): Argument #1 ($position) must be greater than or equal to 0
112func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
113func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
114Exception: Too few arguments to function test3(), 1 passed in %s on line %d and exactly 2 expected
115int(1)
116int(2)
117func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
118int(1)
119func_get_arg(): Argument #1 ($position) must be less than the number of the arguments passed to the currently executed function
120func_get_arg() cannot be called from the global scope
121Done
122