PHP开发笔记's Archiver

xiexie 发表于 2023-2-6 09:30

自动加载如何与 Composer 配合使用

<p style="box-sizing: inherit; margin-bottom: 20px; line-height: 1.8; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px;">如果您想按照示例进行操作,请确保在您的系统上安装 Composer。使用 Composer 自动加载时,您可以选择不同的方法。</p><p style="box-sizing: inherit; margin-bottom: 20px; line-height: 1.8; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px;">具体来说,Composer 提供了四种不同的自动加载文件的方法:</p><ol style="box-sizing: inherit; margin: 30px 0px 20px; padding-left: 2em; list-style-position: outside; list-style-image: initial; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px;"><li style="box-sizing: inherit; margin: 5px 0px 10px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background: transparent;">文件自动加载</li><li style="box-sizing: inherit; margin: 5px 0px 10px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background: transparent;">类图自动加载</li><li style="box-sizing: inherit; margin: 5px 0px 10px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background: transparent;">PSR-0 自动加载</li><li style="box-sizing: inherit; margin: 5px 0px 10px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background: transparent;">PSR-4 自动加载</li></ol><p style="box-sizing: inherit; margin-bottom: 20px; line-height: 1.8; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px;">根据 Composer 官方文档,PSR-4 是推荐的自动加载方式,我们将在下一节详细介绍。在本节中,我们将简要讨论其他三个选项。</p><p style="box-sizing: inherit; margin-bottom: 20px; line-height: 1.8; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px;">在我们继续之前,让我们快速浏览一下当您想使用 Composer 自动加载时需要执行的步骤。</p><ul style="box-sizing: inherit; margin-top: 30px; margin-bottom: 20px; padding-left: 2em; list-style-position: outside; list-style-image: none; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px;"><li style="box-sizing: inherit; margin: 5px 0px 10px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background: transparent;">在项目或库的根目录中定义<span style="box-sizing: inherit; font-weight: 600;">composer.json</span>文件。它应该包含基于自动加载类型的指令。</li><li style="box-sizing: inherit; margin: 5px 0px 10px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background: transparent;">运行该<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">composer dump-autoload</code>命令以生成 Composer 将用于自动加载的必要文件。</li><li style="box-sizing: inherit; margin: 5px 0px 10px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background: transparent;"><code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">require 'vendor/autoload.php'</code>在文件顶部包含要使用自动加载的语句。</li></ul><div><font color="#555555" face="Lato, sans-serif"><span style="font-size: 15px;"><b>自动加载:file指令</b></span></font></div><div></div><div><p style="box-sizing: inherit; margin-bottom: 20px; line-height: 1.8; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px;">文件自动加载的工作方式类似于允许您加载整个源文件的<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">include</code>或<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">require</code>语句。<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">files</code>每次应用程序运行时,都会加载该指令引用的所有源文件。这对于加载不使用类的源文件很有用。</p><p style="box-sizing: inherit; margin-bottom: 20px; line-height: 1.8; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px;">要使用文件自动加载,请<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">files</code>在<span style="box-sizing: inherit; font-weight: 600;">composer.json</span>文件的指令中提供文件列表,如以下代码段所示。</p><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
    <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"autoload"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
        <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"files"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">[</span><span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"lib/Foo.php"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">,</span> <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"lib/Bar.php"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">]</span>
    <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span>
<span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span></code></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">我们可以在</span><code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; white-space: normal;">files</code><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">要使用 Composer 自动加载的指令中提供文件列表。使用上述内容在项目根目录中创建</span><span style="box-sizing: inherit; font-weight: 600; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">composer.json</span><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">文件后,您只需运行该</span><code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; white-space: normal;">composer dump-autoload</code><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">&nbsp;命令即可创建必要的自动加载器文件。这些将在</span><span style="box-sizing: inherit; font-weight: 600; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">供应商</span><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">目录下创建。最后,您需要</span><code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; white-space: normal;">require 'vendor/autoload.php'</code><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">&nbsp;在文件顶部包含要使用 Composer 自动加载文件的语句,如以下代码段所示。</span></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token php language-php" style="box-sizing: inherit; background: transparent !important;"><span class="token delimiter important" style="box-sizing: inherit; color: rgb(253, 151, 31); background: transparent !important;">&lt;?php</span>
<span class="token keyword" style="box-sizing: inherit; background: transparent !important; color: rgb(102, 217, 239);">require</span> <span class="token single-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">'vendor/autoload.php'</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">;</span>
  
<span class="token comment" style="box-sizing: inherit; background: transparent !important; color: slategray;">// code which uses things declared in the "lib/Foo.php" or "lib/Bar.php" file</span>
<span class="token delimiter important" style="box-sizing: inherit; color: rgb(253, 151, 31); background: transparent !important;">?&gt;</span></span></code></pre></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"="">该<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; white-space: normal;">require 'vendor/autoload.php'</code><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">语句确保动态加载必要的文件。</span></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;"><b>自动加载:classmap指令</b></span></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"="">Classmap 自动加载是文件自动加载的改进版本。您只需要提供一个目录列表,Composer 将扫描这些目录中的所有文件。对于每个文件,Composer 将列出该文件中包含的类,并且每当需要这些类中的一个时,Composer 将自动加载相应的文件。</pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">让我们快速修改</span><span style="box-sizing: inherit; font-weight: 600; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">composer.json</span><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">文件来演示类映射自动加载器。</span></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
    <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"autoload"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
        <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"classmap"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">[</span><span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"lib"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">]</span>
    <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span>
<span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span></code></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">运行该<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; white-space: normal;">composer dump-autoload</code><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">命令,Composer 将读取</span><span style="box-sizing: inherit; font-weight: 600; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">lib</span><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">目录中的文件以创建可自动加载的类映射。</span></span></code></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;"><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;"><b>自动加载:PRS-0</b></span></span></code></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;"><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">PSR-0 是 PHP-FIG 组推荐的自动加载标准。在 PSR-0 标准中,您必须使用命名空间来定义您的库。完全限定的类名必须反映<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">\&lt;Vendor Name&gt;\(&lt;Namespace&gt;\)*&lt;Class Name&gt;</code>结构。此外,您的类必须保存在遵循与命名空间相同的目录结构的文件中。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">让我们看看下面的<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">composer.json</code>文件。</p><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
    <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"autoload"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
        <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"psr-0"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
            <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"Tutsplus\\Library"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"src"</span>
        <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span>
    <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span>
<span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span></code></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;"><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">在 PSR-0 自动加载中,您需要将命名空间映射到目录。在上面的例子中,我们告诉 Composer 任何以<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">Tutsplus\Library</code>命名空间开头的东西都应该在<span style="box-sizing: inherit; font-weight: 600;">src\Tutsplus\Library</span>目录中可用。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">例如,如果要<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">Foo</code>在<span style="box-sizing: inherit; font-weight: 600;">src\Tutsplus\Library</span>目录中定义类,则需要创建<span style="box-sizing: inherit; font-weight: 600;">src\Tutsplus\Library\Foo.php</span>&nbsp;文件,如以下代码段所示:</p><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token php language-php" style="box-sizing: inherit; background: transparent !important;"><span class="token delimiter important" style="box-sizing: inherit; color: rgb(253, 151, 31); background: transparent !important;">&lt;?php</span>
<span class="token keyword" style="box-sizing: inherit; background: transparent !important; color: rgb(102, 217, 239);">namespace</span> <span class="token package" style="box-sizing: inherit; background: transparent !important;">Tutsplus<span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">\</span>Library</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">;</span>

<span class="token keyword" style="box-sizing: inherit; background: transparent !important; color: rgb(102, 217, 239);">class</span> <span class="token class-name" style="box-sizing: inherit; background: transparent !important; color: rgb(230, 219, 116);">Foo</span>
<span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
    <span class="token comment" style="box-sizing: inherit; background: transparent !important; color: slategray;">//...</span>
<span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span>
<span class="token delimiter important" style="box-sizing: inherit; color: rgb(253, 151, 31); background: transparent !important;">?&gt;</span></span></code></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">这个类是在</span><code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; white-space: normal;">Tutsplus\Library</code><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">命名空间中定义的。此外,文件名对应于类名。让我们快速看看如何自动加载</span><code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; white-space: normal;">Foo</code><span style="color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">类。</span></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token php language-php" style="box-sizing: inherit; background: transparent !important;"><span class="token delimiter important" style="box-sizing: inherit; color: rgb(253, 151, 31); background: transparent !important;">&lt;?php</span>
<span class="token keyword" style="box-sizing: inherit; background: transparent !important; color: rgb(102, 217, 239);">require</span> <span class="token single-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">'vendor/autoload.php'</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">;</span>
  
<span class="token variable" style="box-sizing: inherit; background: transparent !important;">$objFoo</span> <span class="token operator" style="box-sizing: inherit; background: transparent !important;">=</span> <span class="token keyword" style="box-sizing: inherit; background: transparent !important; color: rgb(102, 217, 239);">new</span> <span class="token class-name" style="box-sizing: inherit; background: transparent !important; color: rgb(230, 219, 116);">Tutsplus<span class="token punctuation" style="box-sizing: inherit; background: transparent !important; color: rgb(248, 248, 242);">\</span>Library<span class="token punctuation" style="box-sizing: inherit; background: transparent !important; color: rgb(248, 248, 242);">\</span>Foo</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">(</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">)</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">;</span>
<span class="token delimiter important" style="box-sizing: inherit; color: rgb(253, 151, 31); background: transparent !important;">?&gt;</span></span></code></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token php language-php" style="box-sizing: inherit; background: transparent !important;"><span class="token delimiter important" style="box-sizing: inherit; color: rgb(253, 151, 31); background: transparent !important;"><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">Composer 将从<span style="box-sizing: inherit; font-weight: 600;">src\Tutsplus\Library</span>目录自动加载<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">Foo</code>类。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">以上是对 Composer 中文件、类映射和 PSR-0 自动加载的简要说明。在下一节中,我们将看到 PSR-4 自动加载是如何工作的。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;"><b>自动加载:PRS-4</b></p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">在上一节中,我们讨论了 PSR-0 自动加载的工作原理。PSR-4 类似于 PSR-0 自动加载,因为您需要使用名称空间,但您不需要使用名称空间模仿目录结构。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">在 PSR-0 自动加载中,您必须将命名空间映射到目录结构。正如我们在上一节中讨论的,如果您想自动加载<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">Tutsplus\Library\Foo</code>类,它必须位于<span style="box-sizing: inherit; font-weight: 600;">src\Tutsplus\Library\Foo.php</span>。在 PSR-4 自动加载中,您可以缩短目录结构,与 PSR-0 自动加载相比,这会导致目录结构更加简单。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">我们将修改上面的示例 – 看看您是否能发现差异。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">这是使用 PSR-4 自动加载的&nbsp;<span style="box-sizing: inherit; font-weight: 600;">composer.json</span>文件的外观。</p><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
    <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"autoload"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
        <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"psr-4"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">{</span>
            <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"Tutsplus\\Library\\"</span><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">:</span> <span class="token double-quoted-string string" style="box-sizing: inherit; background: transparent !important; color: rgb(166, 226, 46);">"src"</span>
        <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span>
    <span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span>
<span class="token punctuation" style="box-sizing: inherit; background: transparent !important;">}</span></code></pre><pre class="line-numbers language-php" style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 1.5; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" overflow:="" auto;="" margin-top:="" 0.5em;="" margin-bottom:="" padding:="" 1em="" 3.8em;="" border:="" 0px;="" background:="" rgb(39,="" 40,="" 34);="" color:="" rgb(248,="" 248,="" 242);="" border-radius:="" 0.3em;="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" word-break:="" normal;="" overflow-wrap:="" tab-size:="" 4;="" hyphens:="" none;="" position:="" relative;="" counter-reset:="" linenumber="" 0;"=""><code class=" language-php" style="box-sizing: inherit; font-family: Consolas, Monaco, " andale="" mono",="" "ubuntu="" monospace;="" font-size:="" 1em;="" font-weight:="" 600;="" background:="" none;="" padding:="" 2px="" 4px;="" border-radius:="" text-shadow:="" rgba(0,="" 0,="" 0.3)="" 0px="" 1px;="" white-space:="" inherit;="" word-spacing:="" normal;="" word-break:="" overflow-wrap:="" line-height:="" 1.5;="" tab-size:="" 4;="" hyphens:="" position:="" relative;"=""><span class="token punctuation" style="box-sizing: inherit; background: transparent !important;"><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">需要注意的是,我们在命名空间的末尾添加了尾随反斜杠。上面的映射告诉 Composer 任何以<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">Tutsplus\Library</code>命名空间开头的东西都应该在<span style="box-sizing: inherit; font-weight: 600;">src</span>目录中可用。所以你不需要创建<span style="box-sizing: inherit; font-weight: 600;">Tutsplus</span>和<span style="box-sizing: inherit; font-weight: 600;">Library</span>目录。例如,如果您请求<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">Tutsplus\Library\Foo</code>该类,Composer 将尝试加载<span style="box-sizing: inherit; font-weight: 600;">src\Foo.php</span>文件。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">重要的是要了解<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">Foo</code>该类仍然定义在<code class="inline" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 13.5px; font-weight: 600; padding: 2px 4px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px;">Tutsplus\Library</code>命名空间下;只是您不需要创建模仿命名空间的目录。该<span style="box-sizing: inherit; font-weight: 600;">SRC \ Foo.php</span>文件的内容将是相同的那些的&nbsp;<span style="box-sizing: inherit; font-weight: 600;">SRC \ Tutsplus \图书馆\ Foo.php</span>的文件<span style="box-sizing: inherit; font-weight: 600;">&nbsp;</span>一节。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">如您所见,PSR-4 导致目录结构更加简单,因为您可以省略创建嵌套目录,同时仍然使用完整的命名空间。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; overflow-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; white-space: normal;">PSR-4 是推荐的自动加载方式,它被 PHP 社区广泛接受。如果您还没有这样做,您应该开始在您的应用程序中使用它!</p></span></code></pre></span></span></code></pre></pre></span></code></pre></span></code></pre></pre></div>

[[i] 本帖最后由 xiexie 于 2023-2-6 09:41 编辑 [/i]]

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.