xref: /PHP-7.4/ext/spl/tests/spl_autoload_008.phpt (revision 782352c5)
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	try
46	{
47		var_dump($func);
48		spl_autoload_register($func);
49		if (count(spl_autoload_functions()))
50		{
51			echo "registered\n";
52
53			var_dump(class_exists("NoExistingTestClass", true));
54		}
55	}
56	catch (Exception $e)
57	{
58		echo get_class($e) . ": " . $e->getMessage() . "\n";
59	}
60
61	spl_autoload_unregister($func);
62	var_dump(count(spl_autoload_functions()));
63}
64
65?>
66===DONE===
67<?php exit(0); ?>
68--EXPECTF--
69====0====
70string(10) "MyAutoLoad"
71registered
72MyAutoLoad(NoExistingTestClass)
73Exception: Bla
74int(0)
75====1====
76string(22) "MyAutoLoader::autoLoad"
77registered
78MyAutoLoader::autoLoad(NoExistingTestClass)
79Exception: Bla
80int(0)
81====2====
82string(22) "MyAutoLoader::dynaLoad"
83LogicException: Function 'MyAutoLoader::dynaLoad' not callable (non-static method MyAutoLoader::dynaLoad() should not be called statically)
84int(0)
85====3====
86array(2) {
87  [0]=>
88  string(12) "MyAutoLoader"
89  [1]=>
90  string(8) "autoLoad"
91}
92registered
93MyAutoLoader::autoLoad(NoExistingTestClass)
94Exception: Bla
95int(0)
96====4====
97array(2) {
98  [0]=>
99  string(12) "MyAutoLoader"
100  [1]=>
101  string(8) "dynaLoad"
102}
103LogicException: Passed array specifies a non static method but no object (non-static method MyAutoLoader::dynaLoad() should not be called statically)
104int(0)
105====5====
106array(2) {
107  [0]=>
108  object(MyAutoLoader)#%d (0) {
109  }
110  [1]=>
111  string(8) "autoLoad"
112}
113registered
114MyAutoLoader::autoLoad(NoExistingTestClass)
115Exception: Bla
116int(0)
117====6====
118array(2) {
119  [0]=>
120  object(MyAutoLoader)#%d (0) {
121  }
122  [1]=>
123  string(8) "dynaLoad"
124}
125registered
126MyAutoLoader::dynaLoad(NoExistingTestClass)
127Exception: Bla
128int(0)
129===DONE===
130