[英]PHP Composer Autoload difference between classmap and files
What is the difference on autoload of property classmap
vs. files
.属性classmap
与files
的自动加载有什么区别。
They both makes the same thing and work the same way?他们都做同样的事情并以同样的方式工作?
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Computer\\Automobile\\": "automobile/"
},
"classmap": [
"classes/automobile.php"
],
"files": [
"files/automobile.php"
]
}
}
I know the PSR-4 standard requires of you a strict filesystem structure based on namespaces.我知道 PSR-4 标准要求您采用基于命名空间的严格文件系统结构。 Say you have an app
directory in the src
directory with App
namespace, then all sub-namespaces will mirror sub-directories and class names will be the same as file names without the .php
extension.假设您在src
目录中有一个带有App
命名空间的app
目录,那么所有子命名空间都将镜像子目录,并且类名将与不带.php
扩展名的文件名相同。
Those are documented in autoload
reference , in short:这些都记录在autoload
reference中,简而言之:
classmap
: point to one or more files or directories. classmap
:指向一个或多个文件或目录。 when you execute composer dump-autoload
(implicitly with composer install
and composer update
as well), Composer will scan these path(s) for PHP files for classes/interfaces/traits/enums and creates a mapping for those fully qualified names to the files pathnames that contain them.当您执行composer dump-autoload
(也隐式使用composer install
和composer update
)时,Composer 将扫描这些路径以查找 PHP 文件的类/接口/特征/枚举,并为这些文件的完全限定名称创建映射包含它们的路径名。 When the autoloader kicks in, that classmap is checked and then the file required if in the map.当自动加载器启动时,会检查该类映射,然后检查映射中所需的文件。 You can make the classmap authorative, it is highly versatile (compareAutoloader optimization ).您可以使 classmap 具有权威性,它用途广泛(比较Autoloader optimization )。
files
: point to one or more files. files
:指向一个或多个文件。 strictly speaking this is not a class autoloader (compare spl_autoload()
etc. ).严格来说,这不是一个类自动加载器(比较spl_autoload()
等)。 All files pointed to will be loaded (required) when the composer autoloader is required ( vendor/autoload.php
).当需要作曲家自动加载器( vendor/autoload.php
)时,指向的所有文件都将被加载(必需)。 You can use it to load files that define functions as no autoloading exists for that.您可以使用它来加载定义函数的文件,因为不存在自动加载。 It is also often used to create shims (conditionally defined functions or classes if they don't exist, alias classes etc.).它还经常用于创建垫片(条件定义的函数或类,如果它们不存在,别名类等)。
So what is the difference.那么区别是什么呢。 I'd say those you can't compare very well to each other.我会说那些你不能很好地相互比较的人。 Compared with psr-4
thought you could instead use classmap
and just point to the directory.与 psr psr-4
相比,您可以改为使用classmap
并指向目录。 Composer would then do the work at dump-autoload
time.然后 Composer 将在dump-autoload
时完成工作。 This can be useful when building the projects package for deployment.这在构建项目包以进行部署时很有用。
For development, psr-4
is better as you don't need to dump-autoload
all the time when you add a new class or rename one etc.对于开发, psr-4
更好,因为当您添加新类或重命名类等时,您不需要一直dump-autoload
。
A common use-case is to have psr-4
therefore configured.一个常见的用例是配置psr-4
。 Then when you build the package for deployment, you make use of the build-in autoload optimization.然后,当您构建用于部署的包时,您可以使用内置的自动加载优化。 It will automatically dump a classmap
for the psr-4
parts as in production we don't expect the code to change any longer (and the classmap must not check the file-system any longer (disk i/o!) as it is in memory).它将自动转储classmap
psr-4
部件的类映射,因为在生产中我们不希望代码再更改(并且类映射不能再检查文件系统(磁盘 i/o!),因为它在记忆)。 Making the classmap authorative then even means, the file-system won't be checked any longer even as a fall-back.使类映射具有权威性甚至意味着,即使作为后备,也不会再检查文件系统。
Use all of these options to your benefit, be it development or later on in production:使用所有这些选项对您有利,无论是开发还是后期生产:
psr-4
by default.默认使用psr-4
。classmap
if there is no other good option (eg code that does not support psr-4
/ psr-0
(check psr-0
if it suits, it is pretty lax in Composer).如果没有其他好的选项,请使用classmap
(例如不支持psr-4
/ psr-0
的代码(如果适合,请检查psr-0
,它在 Composer 中相当宽松)。files
is for file includes. files
用于文件包含。 You normally know when you need it.您通常知道何时需要它。 Should not be used.不应该使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.