File: type_hinting_003.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (65 lines) | stat: -rw-r--r-- 1,126 bytes parent folder | download
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--
ZE2 class type hinting with arrays
--FILE--
<?php

class Test
{
    static function f1(array $ar)
    {
        echo __METHOD__ . "()\n";
        var_dump($ar);
    }

    static function f2(array $ar = NULL)
    {
        echo __METHOD__ . "()\n";
        var_dump($ar);
    }

    static function f3(array $ar = array())
    {
        echo __METHOD__ . "()\n";
        var_dump($ar);
    }

    static function f4(array $ar = array(25))
    {
        echo __METHOD__ . "()\n";
        var_dump($ar);
    }
}

Test::f1(array(42));
Test::f2(NULL);
Test::f2();
Test::f3();
Test::f4();
Test::f1(1);

?>
--EXPECTF--
Deprecated: Test::f2(): Implicitly marking parameter $ar as nullable is deprecated, the explicit nullable type must be used instead in %s on line %d
Test::f1()
array(1) {
  [0]=>
  int(42)
}
Test::f2()
NULL
Test::f2()
NULL
Test::f3()
array(0) {
}
Test::f4()
array(1) {
  [0]=>
  int(25)
}

Fatal error: Uncaught TypeError: Test::f1(): Argument #1 ($ar) must be of type array, int given, called in %s on line %d and defined in %s:%d
Stack trace:
#0 %s(%d): Test::f1(1)
#1 {main}
  thrown in %s on line %d