language/oop5/autoload.xml
ce3a2d381693ccbc10cc4a808c3eb853f3c85c9e
...
...
@@ -9,43 +9,31 @@
9
9
at the beginning of each script (one for each class).
10
10
</para>
11
11
<para>
12
-
In PHP 5, this is no longer necessary. You may define an
13
-
<function>__autoload</function> function which is automatically
14
-
called in case you are trying to use a class/interface which hasn't been
15
-
defined yet. By calling this function the scripting engine is given
16
-
a last chance to load the class before PHP fails with an error.
12
+
The <function>spl_autoload_register</function> function registers any number of
13
+
autoloaders, enabling for classes and interfaces to be automatically loaded
14
+
if they are currently not defined. By registering autoloaders, PHP is given
15
+
a last chance to load the class or interface before it fails with an error.
17
16
</para>
18
-
<tip>
19
-
<para>
20
-
<function>spl_autoload_register</function> provides a more flexible
21
-
alternative for autoloading classes. For this reason, using
22
-
<function>__autoload</function> is discouraged and may be deprecated or
23
-
removed in the future.
24
-
</para>
25
-
</tip>
26
-
<note>
27
-
<para>
28
-
Prior to 5.3.0, exceptions thrown in the __autoload function could not be
29
-
caught in the <link linkend="language.exceptions">catch</link> block and
30
-
would result in a fatal error. From 5.3.0+ exceptions thrown in the
31
-
__autoload function can be caught in the <link linkend="language.exceptions">
32
-
catch</link> block, with 1 provision. If throwing a custom exception, then
33
-
the custom exception class must be available. The __autoload function may
34
-
be used recursively to autoload the custom exception class.
35
-
</para>
36
-
</note>
37
-
<note>
17
+
<para>
18
+
Any class-like construct may be autoloaded the same way. That includes classes,
19
+
interfaces, traits, and enumerations.
20
+
</para>
21
+
<caution>
38
22
<para>
39
-
Autoloading is not available if using PHP in CLI
40
-
<link linkend="features.commandline">interactive mode</link>.
23
+
Prior to PHP 8.0.0, it was possible to use <function>__autoload</function>
24
+
to autoload classes and interfaces. However, it is a less flexible
25
+
alternative to <function>spl_autoload_register</function> and
26
+
<function>__autoload</function> is deprecated as of PHP 7.2.0, and removed
27
+
as of PHP 8.0.0.
41
28
</para>
42
-
</note>
29
+
</caution>
43
30
<note>
44
31
<para>
45
-
If the class name is used e.g. in <function>call_user_func</function> then
46
-
it can contain some dangerous characters such as <literal>../</literal>.
47
-
It is recommended to not use the user-input in such functions or at least
48
-
verify the input in <function>__autoload</function>.
32
+
<function>spl_autoload_register</function> may be called multiple times in order
33
+
to register multiple autoloaders. Throwing an exception from an autoload function,
34
+
however, will interrupt that process and not allow further autoload functions to
35
+
run. For that reason, throwing exceptions from an autoload function is strongly
36
+
discouraged.
49
37
</para>
50
38
</note>
51
39
<para>
...
...
@@ -59,9 +47,9 @@
59
47
<programlisting role="php">
60
48
<![CDATA[
61
49
<?php
62
-
function __autoload($class_name) {
50
+
spl_autoload_register(function ($class_name) {
63
51
include $class_name . '.php';
64
-
}
52
+
});
65
53

66
54
$obj = new MyClass1();
67
55
$obj2 = new MyClass2();
...
...
@@ -78,9 +66,9 @@ $obj2 = new MyClass2();
78
66
<![CDATA[
79
67
<?php
80
68

81
-
function __autoload($name) {
69
+
spl_autoload_register(function ($name) {
82
70
var_dump($name);
83
-
}
71
+
});
84
72

85
73
class Foo implements ITest {
86
74
}
...
...
@@ -94,66 +82,6 @@ Fatal error: Interface 'ITest' not found in ...
94
82
]]>
95
83
</programlisting>
96
84
</example>
97
-
<example>
98
-
<title>Autoloading with exception handling for 5.3.0+</title>
99
-
<para>
100
-
This example throws an exception and demonstrates the try/catch block.
101
-
</para>
102
-
<programlisting role="php">
103
-
<![CDATA[
104
-
<?php
105
-
function __autoload($name) {
106
-
echo "Want to load $name.\n";
107
-
throw new Exception("Unable to load $name.");
108
-
}
109
-

110
-
try {
111
-
$obj = new NonLoadableClass();
112
-
} catch (Exception $e) {
113
-
echo $e->getMessage(), "\n";
114
-
}
115
-
?>
116
-
]]>
117
-
</programlisting>
118
-
&example.outputs;
119
-
<screen>
120
-
<![CDATA[
121
-
Want to load NonLoadableClass.
122
-
Unable to load NonLoadableClass.
123
-
]]>
124
-
</screen>
125
-
</example>
126
-
<example>
127
-
<title>Autoloading with exception handling for 5.3.0+ - Missing custom exception</title>
128
-
<para>
129
-
This example throws an exception for a non-loadable, custom exception.
130
-
</para>
131
-
<programlisting role="php">
132
-
<![CDATA[
133
-
<?php
134
-
function __autoload($name) {
135
-
echo "Want to load $name.\n";
136
-
throw new MissingException("Unable to load $name.");
137
-
}
138
-

139
-
try {
140
-
$obj = new NonLoadableClass();
141
-
} catch (Exception $e) {
142
-
echo $e->getMessage(), "\n";
143
-
}
144
-
?>
145
-
]]>
146
-
</programlisting>
147
-
&example.outputs;
148
-
<screen>
149
-
<![CDATA[
150
-
Want to load NonLoadableClass.
151
-
Want to load MissingException.
152
-

153
-
Fatal error: Class 'MissingException' not found in testMissingException.php on line 4
154
-
]]>
155
-
</screen>
156
-
</example>
157
85
</para>
158
86

159
87
<simplesect role="seealso">
...
...
@@ -161,9 +89,11 @@ Fatal error: Class 'MissingException' not found in testMissingException.php on l
161
89
<para>
162
90
<simplelist>
163
91
<member><function>unserialize</function></member>
164
-
<member><link linkend="unserialize-callback-func">unserialize_callback_func</link></member>
165
-
<member><function>spl_autoload</function></member>
92
+
<member><link linkend="ini.unserialize-callback-func">unserialize_callback_func</link></member>
93
+
<member><link linkend="ini.unserialize-max-depth">unserialize_max_depth</link></member>
166
94
<member><function>spl_autoload_register</function></member>
95
+
<member><function>spl_autoload</function></member>
96
+
<member><function>__autoload</function></member>
167
97
</simplelist>
168
98
</para>
169
99
</simplesect>
170
100