xref: /PHP-8.1/ext/spl/tests/spl_autoload_001.phpt (revision 99d0f502)
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    spl_autoload_register("unavailable_autoload_function");
69} catch(\TypeError $e) {
70    echo $e->getMessage() . \PHP_EOL;
71}
72
73?>
74--EXPECTF--
75===EMPTY===
76string(9) ".inc,.php"
77%stestclass.inc
78Class TestClass could not be loaded
79===()===
80Class TestClass could not be loaded
81===(1)===
82Class TestClass could not be loaded
83===(.inc,,.php.inc)===
84%stestclass
85%stestclass.php.inc
86Class TestClass could not be loaded
87===()===
88Class TestClass could not be loaded
89Class TestClass could not be loaded
90===SPL_AUTOLOAD()===
91string(4) ".inc"
92bool(false)
93===REGISTER===
94TestFunc1(TestClass)
95TestFunc2(TestClass)
96bool(false)
97===LOAD===
98TestFunc1(TestClass)
99TestFunc2(TestClass)
100%stestclass.class.inc
101bool(true)
102===NOFUNCTION===
103spl_autoload_register(): Argument #1 ($callback) must be a valid callback or null, function "unavailable_autoload_function" not found or invalid function name
104