-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathautoload.xml
executable file
·133 lines (123 loc) · 4.01 KB
/
autoload.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: c1f37a6c270aadbbb3da56a3973ffd62197adf2b Maintainer: dallas Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<sect1 xml:id="language.oop5.autoload" xmlns="http://docbook.org/ns/docbook">
<title>类的自动加载</title>
<para>
在编写面向对象(OOP)程序时,很多开发者为每个类新建一个 PHP 文件。
这会带来一个烦恼:每个脚本的开头,都需要包含(include)一个长长的列表(每个类都有个文件)。
</para>
<para>
<function>spl_autoload_register</function> 函数可以注册任意数量的自动加载器,当使用尚未被定义的类(class)和接口(interface)时自动去加载。通过注册自动加载器,脚本引擎在
PHP 出错失败前有了最后一个机会加载所需的类。
</para>
<para>
像 class 一样的结构都可以以相同方式自动加载。包括类、接口、trait 和枚举。
</para>
<caution>
<para>
PHP 8.0.0 之前,可以使用 <function>__autoload</function> 自动加载类和接口。然而,它是
<function>spl_autoload_register</function> 的一种不太灵活的替代方法,并且
<function>__autoload</function> 在 PHP 7.2.0 起弃用,在 PHP 8.0.0 起移除。
</para>
</caution>
<note>
<para>
<function>spl_autoload_register</function>
可以多次调用以便注册多个自动加载器。但从自动加载函数中抛出异常会中断该过程并且禁止继续执行。因此强烈建议不要从自动加载函数中抛出异常。
</para>
</note>
<para>
<example>
<title>自动加载示例</title>
<para>
本例尝试分别从 <filename>MyClass1.php</filename>
和 <filename>MyClass2.php</filename> 文件中加载
<literal>MyClass1</literal> 和
<literal>MyClass2</literal> 类。
</para>
<programlisting role="php" annotations="interactive">
<![CDATA[
<?php
spl_autoload_register(function ($class_name) {
require_once $class_name . '.php';
});
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
]]>
</programlisting>
</example>
<example>
<title>另一个例子</title>
<para>
本例尝试加载接口 <literal>ITest</literal>。
</para>
<programlisting role="php">
<![CDATA[
<?php
spl_autoload_register(function ($name) {
var_dump($name);
});
class Foo implements ITest {
}
/*
string(5) "ITest"
Fatal error: Interface 'ITest' not found in ...
*/
?>
]]>
</programlisting>
</example>
<example>
<title>使用 Composer 的自动加载器</title>
<simpara>
Composer 会生成 <literal>vendor/autoload.php</literal> 文件,用于自动加载
Composer 管理的软件包。通过 include 此文件,无需任何额外工作即可使用这些软件包。
</simpara>
<programlisting role="php" annotations="interactive">
<![CDATA[
<?php
require __DIR__ . '/vendor/autoload.php';
$uuid = Ramsey\Uuid\Uuid::uuid7();
echo "Generated new UUID -> ", $uuid->toString(), "\n";
?>
]]>
</programlisting>
</example>
</para>
<simplesect role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>unserialize</function></member>
<member><link linkend="ini.unserialize-callback-func">unserialize_callback_func</link></member>
<member><link linkend="ini.unserialize-max-depth">unserialize_max_depth</link></member>
<member><function>spl_autoload_register</function></member>
<member><function>spl_autoload</function></member>
<member><function>__autoload</function></member>
</simplelist>
</para>
</simplesect>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->