language/oop5/autoload.xml
c1f37a6c270aadbbb3da56a3973ffd62197adf2b
c1f37a6c270aadbbb3da56a3973ffd62197adf2b
...
...
@@ -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>
...
...
@@ -56,12 +44,12 @@
56
44
and <literal>MyClass2</literal> from the files <filename>MyClass1.php</filename>
57
45
and <filename>MyClass2.php</filename> respectively.
58
46
</para>
59
-
<programlisting role="php">
47
+
<programlisting role="php" annotations="interactive">
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
}
...
...
@@ -95,64 +83,24 @@ Fatal error: Interface 'ITest' not found in ...
95
83
</programlisting>
96
84
</example>
97
85
<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">
86
+
<title>Using Composer's autoloader</title>
87
+
<simpara>
88
+
&link.composer; generates a <literal>vendor/autoload.php</literal>
89
+
which is set up to automatically load packages that are being managed
90
+
by Composer. By including this file, those packages can be used without
91
+
any additional work.
92
+
</simpara>
93
+
<programlisting role="php" annotations="interactive">
103
94
<![CDATA[
104
95
<?php
105
-
function __autoload($name) {
106
-
echo "Want to load $name.\n";
107
-
throw new Exception("Unable to load $name.");
108
-
}
96
+
require __DIR__ . '/vendor/autoload.php';
109
97
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
-
}
98
+
$uuid = Ramsey\Uuid\Uuid::uuid7();
138
99
139
-
try {
140
-
$obj = new NonLoadableClass();
141
-
} catch (Exception $e) {
142
-
echo $e->getMessage(), "\n";
143
-
}
100
+
echo "Generated new UUID -> ", $uuid->toString(), "\n";
144
101
?>
145
102
]]>
146
103
</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
104
</example>
157
105
</para>
158
106
...
...
@@ -161,9 +109,11 @@ Fatal error: Class 'MissingException' not found in testMissingException.php on l
161
109
<para>
162
110
<simplelist>
163
111
<member><function>unserialize</function></member>
164
-
<member><link linkend="unserialize-callback-func">unserialize_callback_func</link></member>
165
-
<member><function>spl_autoload</function></member>
112
+
<member><link linkend="ini.unserialize-callback-func">unserialize_callback_func</link></member>
113
+
<member><link linkend="ini.unserialize-max-depth">unserialize_max_depth</link></member>
166
114
<member><function>spl_autoload_register</function></member>
115
+
<member><function>spl_autoload</function></member>
116
+
<member><function>__autoload</function></member>
167
117
</simplelist>
168
118
</para>
169
119
</simplesect>
170
120