xref: /PHP-8.1/ext/spl/tests/spl_autoload_007.phpt (revision 99d0f502)
1--TEST--
2SPL: spl_autoload() with inaccessible methods
3--INI--
4include_path=.
5--FILE--
6<?php
7
8class MyAutoLoader {
9
10        static protected function noAccess($className) {
11            echo __METHOD__ . "($className)\n";
12        }
13
14        static function autoLoad($className) {
15            echo __METHOD__ . "($className)\n";
16        }
17
18        function dynaLoad($className) {
19            echo __METHOD__ . "($className)\n";
20        }
21}
22
23$obj = new MyAutoLoader;
24
25$funcs = array(
26    'MyAutoLoader::notExist',
27    'MyAutoLoader::noAccess',
28    'MyAutoLoader::autoLoad',
29    'MyAutoLoader::dynaLoad',
30    array('MyAutoLoader', 'notExist'),
31    array('MyAutoLoader', 'noAccess'),
32    array('MyAutoLoader', 'autoLoad'),
33    array('MyAutoLoader', 'dynaLoad'),
34    array($obj, 'notExist'),
35    array($obj, 'noAccess'),
36    array($obj, 'autoLoad'),
37    array($obj, 'dynaLoad'),
38);
39
40foreach($funcs as $idx => $func)
41{
42    if ($idx) echo "\n";
43    try {
44        var_dump($func);
45        spl_autoload_register($func);
46        echo "ok\n";
47    } catch(\TypeError $e) {
48        echo $e->getMessage() . \PHP_EOL;
49    }
50}
51
52?>
53--EXPECTF--
54string(22) "MyAutoLoader::notExist"
55spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, class MyAutoLoader does not have a method "notExist"
56
57string(22) "MyAutoLoader::noAccess"
58spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, cannot access protected method MyAutoLoader::noAccess()
59
60string(22) "MyAutoLoader::autoLoad"
61ok
62
63string(22) "MyAutoLoader::dynaLoad"
64spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, non-static method MyAutoLoader::dynaLoad() cannot be called statically
65
66array(2) {
67  [0]=>
68  string(12) "MyAutoLoader"
69  [1]=>
70  string(8) "notExist"
71}
72spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, class MyAutoLoader does not have a method "notExist"
73
74array(2) {
75  [0]=>
76  string(12) "MyAutoLoader"
77  [1]=>
78  string(8) "noAccess"
79}
80spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, cannot access protected method MyAutoLoader::noAccess()
81
82array(2) {
83  [0]=>
84  string(12) "MyAutoLoader"
85  [1]=>
86  string(8) "autoLoad"
87}
88ok
89
90array(2) {
91  [0]=>
92  string(12) "MyAutoLoader"
93  [1]=>
94  string(8) "dynaLoad"
95}
96spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, non-static method MyAutoLoader::dynaLoad() cannot be called statically
97
98array(2) {
99  [0]=>
100  object(MyAutoLoader)#%d (0) {
101  }
102  [1]=>
103  string(8) "notExist"
104}
105spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, class MyAutoLoader does not have a method "notExist"
106
107array(2) {
108  [0]=>
109  object(MyAutoLoader)#%d (0) {
110  }
111  [1]=>
112  string(8) "noAccess"
113}
114spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, cannot access protected method MyAutoLoader::noAccess()
115
116array(2) {
117  [0]=>
118  object(MyAutoLoader)#%d (0) {
119  }
120  [1]=>
121  string(8) "autoLoad"
122}
123ok
124
125array(2) {
126  [0]=>
127  object(MyAutoLoader)#%d (0) {
128  }
129  [1]=>
130  string(8) "dynaLoad"
131}
132ok
133