language/oop5/autoload.xml
ce3a2d381693ccbc10cc4a808c3eb853f3c85c9e
...
...
@@ -9,46 +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. The
13
-
<function>spl_autoload_register</function> function registers any number of
12
+
The <function>spl_autoload_register</function> function registers any number of
14
13
autoloaders, enabling for classes and interfaces to be automatically loaded
15
14
if they are currently not defined. By registering autoloaders, PHP is given
16
15
a last chance to load the class or interface before it fails with an error.
17
16
</para>
18
-
<tip>
19
-
<para>
20
-
Although the <function>__autoload</function> function can also be used for
21
-
autoloading classes and interfaces, it's preferred to use the
22
-
<function>spl_autoload_register</function> function. This is because it is
23
-
a more flexible alternative (enabling for any number of autoloaders to be
24
-
specified in the application, such as in third party libraries). For this
25
-
reason, using <function>__autoload</function> is discouraged and it may be
26
-
deprecated in the future.
27
-
</para>
28
-
</tip>
29
-
<note>
30
-
<para>
31
-
Prior to PHP 5.3, exceptions thrown in the <function>__autoload</function>
32
-
function could not be caught in the
33
-
<link linkend="language.exceptions">catch</link> block and would result in
34
-
a fatal error. From PHP 5.3 and upwards, this is possible provided that if
35
-
a custom exception is thrown, then the custom exception class is available.
36
-
The <function>__autoload</function> function may be used recursively to
37
-
autoload the custom exception class.
38
-
</para>
39
-
</note>
40
-
<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>
41
22
<para>
42
-
Autoloading is not available if using PHP in CLI
43
-
<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.
44
28
</para>
45
-
</note>
29
+
</caution>
46
30
<note>
47
31
<para>
48
-
If the class name is used e.g. in <function>call_user_func</function> then
49
-
it can contain some dangerous characters such as <literal>../</literal>.
50
-
It is recommended to not use the user-input in such functions or at least
51
-
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.
52
37
</para>
53
38
</note>
54
39
<para>
...
...
@@ -97,66 +82,6 @@ Fatal error: Interface 'ITest' not found in ...
97
82
]]>
98
83
</programlisting>
99
84
</example>
100
-
<example>
101
-
<title>Autoloading with exception handling for 5.3.0+</title>
102
-
<para>
103
-
This example throws an exception and demonstrates the try/catch block.
104
-
</para>
105
-
<programlisting role="php">
106
-
<![CDATA[
107
-
<?php
108
-
spl_autoload_register(function ($name) {
109
-
echo "Want to load $name.\n";
110
-
throw new Exception("Unable to load $name.");
111
-
});
112
-

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

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

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

162
87
<simplesect role="seealso">
...
...
@@ -164,7 +89,8 @@ Fatal error: Class 'MissingException' not found in testMissingException.php on l
164
89
<para>
165
90
<simplelist>
166
91
<member><function>unserialize</function></member>
167
-
<member><link linkend="unserialize-callback-func">unserialize_callback_func</link></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>
168
94
<member><function>spl_autoload_register</function></member>
169
95
<member><function>spl_autoload</function></member>
170
96
<member><function>__autoload</function></member>
171
97