ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# composer自动加载讲解 > 总结: PSR-0与PSR-4自动加载格式: root命名空间:起始路径(从composer.json同级目录算起) ``` <pre class="calibre10">``` <span class="token2">"psr-0 | 4"</span><span class="token3">:</span><span class="token3">{</span> <span class="token2">"root命名空间\\"</span><span class="token3">:</span><span class="token2">"起始路径"</span><span class="token3">,</span> <span class="token3">}</span><span class="token3">,</span> ``` ``` ## **PSR-0:** 例子: ``` <pre class="calibre10">``` <span class="token2">"psr-0"</span><span class="token3">:</span><span class="token3">{</span> <span class="token2">"church\\"</span><span class="token3">:</span><span class="token2">"./src/"</span><span class="token3">,</span> <span class="token3">}</span><span class="token3">,</span> ``` ``` 此定义的类文件必须定义在composer.json同级目录下的src/church/文件夹下: > 起始路径+root命名空间 + \[额外路径\] + 类文件(必须与类名完全一致) > 不光是类文件,还有文件夹这些都对大小写敏感 > 实例化调用时记得加载composer的autoload.php ``` <pre class="calibre10">``` test 应用目录 ├─src目录 │ ├─church目录 │ │ ├─TestClass1<span class="token3">.</span>php │ │ ├─TestClass2<span class="token3">.</span>php │ │ └─ <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> 更多类文件 │ └─ ├─composer<span class="token3">.</span>json composer 定义文件 ``` ``` 定义映射的类: > 类名与文件名完全一致 且 类名必须声明 namespace root命名空间\[\\额外路径 ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php namespace church<span class="token3">;</span> class <span class="token4">TestClass1</span><span class="token3">{</span> <span class="token3">}</span> ``` ``` 使用: > use root命名空间\[\\额外路径\]\\类名; > new 类名(); 或者完全限定 > new \\root命名空间\[\\额外路径\]\\类名() ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php <span class="token">//注意是文件与的类文件及类大小写一致,不只是其中一个</span> use church\TestClass1<span class="token3">;</span> <span class="token5">new</span> <span class="token4">TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//或者直接new</span> <span class="token5">new</span> <span class="token4"><span class="token3">\</span>church<span class="token3">\</span>TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` 例子2: ``` <pre class="calibre10">``` <span class="token">//类文件必须定义在composer.json同级目录下的src/church/Dash/文件夹下</span> <span class="token2">"psr-0"</span><span class="token3">:</span><span class="token3">{</span> <span class="token2">"church\\Dash\\"</span><span class="token3">:</span><span class="token2">"./src/"</span><span class="token3">,</span> <span class="token3">}</span><span class="token3">,</span> ``` ``` 定义: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php namespace church\Dash<span class="token3">;</span> class <span class="token4">TestClass1</span><span class="token3">{</span> <span class="token3">}</span> ``` ``` 使用: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php <span class="token">//注意是文件与的类文件及类大小写一致,不只是其中一个</span> use church\Dash\TestClass1<span class="token3">;</span> <span class="token5">new</span> <span class="token4">TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//或者直接new</span> <span class="token5">new</span> <span class="token4"><span class="token3">\</span>church<span class="token3">\</span>Dash<span class="token3">\</span>TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` 例子3同理: ``` <pre class="calibre10">``` <span class="token">//类文件必须定义在composer.json同级目录下的Church/文件夹下</span> <span class="token2">"psr-0"</span><span class="token3">:</span><span class="token3">{</span> <span class="token2">"Church\\"</span><span class="token3">:</span><span class="token2">""</span><span class="token3">,</span> <span class="token3">}</span><span class="token3">,</span> ``` ``` 定义: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php namespace Church<span class="token3">;</span> class <span class="token4">TestClass1</span><span class="token3">{</span> <span class="token3">}</span> ``` ``` 使用: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php <span class="token">//注意是文件与的类文件及类大小写一致,不只是其中一个</span> use Church\TestClass1<span class="token3">;</span> <span class="token5">new</span> <span class="token4">TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//或者直接new</span> <span class="token5">new</span> <span class="token4"><span class="token3">\</span>Church<span class="token3">\</span>TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ## **PSR-4:** > PSR-4 相对于PSR-0 在对应文件路径时可以省略root命名空间 对应文件路径: > 起始路径+ \[额外路径\] + 类文件(必须与类名一致) 例子1: ``` <pre class="calibre10">``` <span class="token2">"psr-4"</span><span class="token3">:</span><span class="token3">{</span> <span class="token">//"root命名空间\\":"起始路径", 例root命名空间 "Dash\\"或者"Dash\\test\\"</span> <span class="token2">"Dash\\"</span><span class="token3">:</span><span class="token2">"./src/"</span><span class="token3">,</span> <span class="token3">}</span><span class="token3">,</span> ``` ``` 此定义的类文件必须定义在composer.json同级目录下的src及其子文件夹下 > src/church/TestClass1.php 定义: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php namespace Dash<span class="token1">/</span>church<span class="token3">;</span> class <span class="token4">TestClass1</span><span class="token3">{</span> <span class="token3">}</span> ``` ``` 使用: > use root命名空间\[\\额外路径\]\\类名 > new \\root命名空间\[\\额外路径\]\\类名() ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php use Dash\church\TestClass1<span class="token3">;</span> <span class="token5">new</span> <span class="token4">TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//或者直接new</span> <span class="token5">new</span> <span class="token4"><span class="token3">\</span>Dash<span class="token3">\</span>church<span class="token3">\</span>TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` > src/TestClass1.php 定义: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php namespace Dash<span class="token3">;</span> class <span class="token4">TestClass1</span><span class="token3">{</span> <span class="token3">}</span> ``` ``` 使用: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php use Dash\TestClass1<span class="token3">;</span> <span class="token5">new</span> <span class="token4">TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//或者直接new</span> <span class="token5">new</span> <span class="token4"><span class="token3">\</span>Dash<span class="token3">\</span>TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` 例子2: ``` <pre class="calibre10">``` <span class="token2">"psr-4"</span><span class="token3">:</span><span class="token3">{</span> <span class="token">//"root命名空间\\":"起始路径", </span> <span class="token2">"Dash\\tools\\"</span><span class="token3">:</span><span class="token2">"./src/"</span><span class="token3">,</span> <span class="token3">}</span><span class="token3">,</span> ``` ``` 此定义的类文件必须定义在composer.json同级目录下的src及其子文件夹下 > src/TestClass1.php 定义: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php namespace Dash\tools<span class="token3">;</span> class <span class="token4">TestClass1</span><span class="token3">{</span> <span class="token3">}</span> ``` ``` 使用: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php use Dash\tools\TestClass1<span class="token3">;</span> <span class="token5">new</span> <span class="token4">TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//或者直接new</span> <span class="token5">new</span> <span class="token4"><span class="token3">\</span>Dash<span class="token3">\</span>tools<span class="token3">\</span>TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` > src/church/TestClass1.php 定义: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php namespace Dash\tools\church<span class="token3">;</span> class <span class="token4">TestClass1</span><span class="token3">{</span> <span class="token3">}</span> ``` ``` 使用: ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php use Dash\tools\church\TestClass1<span class="token3">;</span> <span class="token5">new</span> <span class="token4">TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//或者直接new</span> <span class="token5">new</span> <span class="token4"><span class="token3">\</span>Dash<span class="token3">\</span>tools<span class="token3">\</span>church<span class="token3">\</span>TestClass1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` psr-4比psr-0路径更简洁 ## **files** > 一般用来自动加载函数、配置等非类文件 ``` <pre class="calibre10">``` <span class="token2">"files"</span><span class="token3">:</span><span class="token3">[</span> <span class="token2">"src/MyLibrary/functions.php"</span> <span class="token3">]</span><span class="token3">,</span> <span class="token">//以上定义会自动加载与composer.json同文件夹下的src/MyLibrary/functions.php</span> ``` ``` ## **classmap** > 适合没有命名空间的类 > 定义后必须要有src和lib文件夹 ``` <pre class="calibre10">``` <span class="token2">"classmap"</span><span class="token3">:</span><span class="token3">[</span> <span class="token2">"src/"</span><span class="token3">,</span> <span class="token2">"lib/"</span> <span class="token3">]</span> ``` ``` > 当Composer开始安装和更新扩展的时候,会根据composer.json里的这个autoload告诉的方式classmap, > 来遍历src、lib目录然后将里面的类文件和其路径一一对应,存放到vendor/composer/autoload\_classmap.php内 > src/文件下必须有文件并且定义了类才会被加载到classmap 总览: ``` <pre class="calibre17">``` <span class="token2">"autoload"</span><span class="token3">:</span><span class="token3">{</span> <span class="token2">"psr-0"</span><span class="token3">:</span><span class="token3">{</span> <span class="token">//简单理解将./src路径替换为church 所以 src里的类文件必须有namespace church </span> <span class="token2">"church\\"</span><span class="token3">:</span><span class="token2">"./src/"</span><span class="token3">,</span> #use church\testClass<span class="token3">,</span> 那就对应src<span class="token1">/</span>church<span class="token1">/</span>testClass<span class="token3">.</span>php<span class="token3">.</span> #use church\test\testClass<span class="token3">,</span> 那就对应src<span class="token1">/</span>church<span class="token1">/</span>test<span class="token1">/</span>testClass<span class="token3">.</span>php <span class="token2">"Test\\"</span> <span class="token3">:</span><span class="token2">"core/"</span><span class="token3">,</span> #composer update后 <span class="token3">.</span><span class="token1">/</span>vender<span class="token1">/</span>composer<span class="token1">/</span>autoload_psr4<span class="token3">.</span>php的 数组里会多出<span class="token2">'Test\\'</span> <span class="token1">=</span><span class="token1">></span> <span class="token4">array</span><span class="token3">(</span>$baseDir <span class="token3">.</span> <span class="token2">'/core'</span><span class="token3">)</span><span class="token3">,</span> <span class="token2">"UniqueGlobalClass"</span><span class="token3">:</span> <span class="token2">""</span> # 这个类的php源文件也位于包的根目录 <span class="token3">}</span><span class="token3">,</span> <span class="token2">"psr-4"</span><span class="token3">:</span><span class="token3">{</span> <span class="token2">"church\\"</span><span class="token3">:</span><span class="token2">"./src/"</span><span class="token3">,</span> # use church\TestClass<span class="token3">,</span> 那就对应src<span class="token1">/</span>testClass<span class="token3">.</span>php<span class="token3">.</span> # use church\Test\TestClass<span class="token3">,</span> 那就对应src<span class="token1">/</span>test<span class="token1">/</span>testClass<span class="token3">.</span>php # <span class="token2">"Dash\\"</span><span class="token3">:</span><span class="token3">[</span><span class="token2">"extra/"</span><span class="token3">,</span><span class="token2">"library/"</span><span class="token3">]</span><span class="token3">,</span> #多个目录中一个相同的<span class="token3">(</span>命名空间<span class="token3">)</span>前缀 psr<span class="token1">-</span><span class="token6">0</span> 也适用 <span class="token2">""</span><span class="token3">:</span><span class="token2">"any/"</span> #任何命名空间<span class="token3">(</span>即实例化类时其他地方未定义,那么就会进入any查找<span class="token3">)</span> 设置一个目录作为任何命名空间的备用目录psr<span class="token1">-</span><span class="token6">0</span>也适用 <span class="token2">"thinkcms\\auth\\"</span><span class="token3">:</span> <span class="token2">""</span> <span class="token">//加载此composer.json同及目录及其子目录下的的所有文件?</span> <span class="token3">}</span><span class="token3">,</span> <span class="token2">"files"</span><span class="token3">:</span><span class="token3">[</span> <span class="token2">"src/MyLibrary/functions.php"</span> #一般用来自动加载函数、配置等非类文件 <span class="token3">]</span><span class="token3">,</span> #当Composer开始安装和更新扩展的时候,会根据composer<span class="token3">.</span>json里的这个autoload告诉的方式classmap, #来遍历src、lib目录然后将里面的类文件和其路径一一对应,存放到vendor<span class="token1">/</span>composer<span class="token1">/</span>autoload_classmap<span class="token3">.</span>php内 #src<span class="token1">/</span>文件下必须有文件并且定义了类才会被加载到classmap <span class="token2">"classmap"</span><span class="token3">:</span><span class="token3">[</span> <span class="token2">"src/"</span><span class="token3">,</span> <span class="token2">"lib/"</span> <span class="token3">]</span> <span class="token3">}</span> ``` ```