mirror of
https://github.com/php/doc-zh.git
synced 2026-03-23 22:52:08 +01:00
* Fix syntax errors in more examples * Add new PHP ini settings for zend.max_allowed_stack_size, zend.reserved_stack_size, and fiber.stack_size * sync commit id
134 lines
4.0 KiB
XML
Executable File
134 lines
4.0 KiB
XML
Executable File
<?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
|
||
-->
|