xref: /PHP-5.5/ext/spl/tests/spl_autoload_001.phpt (revision 610c7fbe)
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
12try
13{
14	spl_autoload("TestClass");
15}
16catch(Exception $e)
17{
18	echo 'Exception: ' . $e->getMessage() . "\n";
19}
20
21$test_exts = array(NULL, "1", ".inc,,.php.inc", "");
22
23foreach($test_exts as $exts)
24{
25	echo "===($exts)===\n";
26	try
27	{
28		spl_autoload("TestClass", $exts);
29	}
30	catch(Exception $e)
31	{
32		echo 'Exception: ' . $e->getMessage() . "\n";
33	}
34}
35
36try
37{
38	spl_autoload_extensions(".inc,.php.inc");
39	spl_autoload("TestClass");
40}
41catch(Exception $e)
42{
43	echo 'Exception: ' . $e->getMessage() . "\n";
44}
45
46function TestFunc1($classname)
47{
48	echo __METHOD__ . "($classname)\n";
49}
50
51function TestFunc2($classname)
52{
53	echo __METHOD__ . "($classname)\n";
54}
55
56echo "===SPL_AUTOLOAD()===\n";
57
58spl_autoload_register();
59
60try
61{
62	var_dump(spl_autoload_extensions(".inc"));
63	var_dump(class_exists("TestClass", true));
64}
65catch(Exception $e)
66{
67	echo 'Exception: ' . $e->getMessage() . "\n";
68}
69
70echo "===REGISTER===\n";
71
72spl_autoload_unregister("spl_autoload");
73spl_autoload_register("TestFunc1");
74spl_autoload_register("TestFunc2");
75spl_autoload_register("TestFunc2"); /* 2nd call ignored */
76spl_autoload_extensions(".inc,.class.inc"); /* we do not have spl_autoload_registered yet */
77
78try
79{
80	var_dump(class_exists("TestClass", true));
81}
82catch(Exception $e)
83{
84	echo 'Exception: ' . $e->getMessage() . "\n";
85}
86
87echo "===LOAD===\n";
88
89spl_autoload_register("spl_autoload");
90var_dump(class_exists("TestClass", true));
91
92echo "===NOFUNCTION===\n";
93
94try
95{
96	spl_autoload_register("unavailable_autoload_function");
97}
98catch(Exception $e)
99{
100	echo 'Exception: ' . $e->getMessage() . "\n";
101}
102
103?>
104===DONE===
105<?php exit(0); ?>
106--EXPECTF--
107===EMPTY===
108string(9) ".inc,.php"
109%stestclass.inc
110Exception: Class TestClass could not be loaded
111===()===
112Exception: Class TestClass could not be loaded
113===(1)===
114Exception: Class TestClass could not be loaded
115===(.inc,,.php.inc)===
116%stestclass
117%stestclass.php.inc
118Exception: Class TestClass could not be loaded
119===()===
120Exception: Class TestClass could not be loaded
121Exception: Class TestClass could not be loaded
122===SPL_AUTOLOAD()===
123string(4) ".inc"
124Exception: Class TestClass could not be loaded
125===REGISTER===
126TestFunc1(TestClass)
127TestFunc2(TestClass)
128bool(false)
129===LOAD===
130TestFunc1(TestClass)
131TestFunc2(TestClass)
132%stestclass.class.inc
133bool(true)
134===NOFUNCTION===
135Exception: Function 'unavailable_autoload_function' not found (function 'unavailable_autoload_function' not found or invalid function name)
136===DONE===
137