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