xref: /PHP-7.4/ext/spl/tests/spl_autoload_001.phpt (revision 8023204d)
1--TEST--
2SPL: spl_autoload() and friends
3--INI--
4include_path=.
5--FILE--
6<?php
7
8echo "===EMPTY===\n";
9
10var_dump(spl_autoload_extensions());
11
12spl_autoload("TestClass");
13if (!class_exists("TestClass")) {
14    echo "Class TestClass could not be loaded\n";
15}
16
17$test_exts = array(NULL, "1", ".inc,,.php.inc", "");
18
19foreach($test_exts as $exts) {
20	echo "===($exts)===\n";
21    spl_autoload("TestClass", $exts);
22    if (!class_exists("TestClass")) {
23        echo "Class TestClass could not be loaded\n";
24    }
25}
26
27spl_autoload_extensions(".inc,.php.inc");
28spl_autoload("TestClass");
29if (!class_exists("TestClass")) {
30    echo "Class TestClass could not be loaded\n";
31}
32
33function TestFunc1($classname)
34{
35	echo __METHOD__ . "($classname)\n";
36}
37
38function TestFunc2($classname)
39{
40	echo __METHOD__ . "($classname)\n";
41}
42
43echo "===SPL_AUTOLOAD()===\n";
44
45spl_autoload_register();
46
47var_dump(spl_autoload_extensions(".inc"));
48var_dump(class_exists("TestClass", true));
49
50echo "===REGISTER===\n";
51
52spl_autoload_unregister("spl_autoload");
53spl_autoload_register("TestFunc1");
54spl_autoload_register("TestFunc2");
55spl_autoload_register("TestFunc2"); /* 2nd call ignored */
56spl_autoload_extensions(".inc,.class.inc"); /* we do not have spl_autoload_registered yet */
57
58var_dump(class_exists("TestClass", true));
59
60echo "===LOAD===\n";
61
62spl_autoload_register("spl_autoload");
63var_dump(class_exists("TestClass", true));
64
65echo "===NOFUNCTION===\n";
66
67try
68{
69	spl_autoload_register("unavailable_autoload_function");
70}
71catch(Exception $e)
72{
73	echo 'Exception: ' . $e->getMessage() . "\n";
74}
75
76?>
77===DONE===
78<?php exit(0); ?>
79--EXPECTF--
80===EMPTY===
81string(9) ".inc,.php"
82%stestclass.inc
83Class TestClass could not be loaded
84===()===
85Class TestClass could not be loaded
86===(1)===
87Class TestClass could not be loaded
88===(.inc,,.php.inc)===
89%stestclass
90%stestclass.php.inc
91Class TestClass could not be loaded
92===()===
93Class TestClass could not be loaded
94Class TestClass could not be loaded
95===SPL_AUTOLOAD()===
96string(4) ".inc"
97bool(false)
98===REGISTER===
99TestFunc1(TestClass)
100TestFunc2(TestClass)
101bool(false)
102===LOAD===
103TestFunc1(TestClass)
104TestFunc2(TestClass)
105%stestclass.class.inc
106bool(true)
107===NOFUNCTION===
108Exception: Function 'unavailable_autoload_function' not found (function 'unavailable_autoload_function' not found or invalid function name)
109===DONE===
110