PHP作曲家自动加载classmap和文件之间的差异
属性 classmap
vs. 文件
的自动加载的区别是什么?
他们都做同样的事情并以同样的方式工作?
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Computer\\Automobile\\": "automobile/"
},
"classmap": [
"classes/automobile.php"
],
"files": [
"files/automobile.php"
]
}
}
我知道您的PSR-4标准需要您基于名称空间的严格文件系统结构。假设您在 src
中有一个 app
,带有 app app
namespace的目录,所有子名称空间都将镜像子目录和类名称为与没有 .php
扩展名的文件名相同。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些记录在
classmap
:指向一个或多个文件或目录。当您执行Composer dump-autoload
(暗中使用Composer install
和COMPOSER UPDATE
)时,作曲家将扫描这些路径的PHP类/接口/特质/枚举的文件,并为包含它们的文件路径名为这些完全合格的名称创建映射。当自动加载器启动时,将检查该clastmap,然后在地图中使用文件。您可以制作ClassMap的权威,它具有高度的用途(比较文件
:指向一个或多个文件。严格来说,这不是类自动加载器(比较spl_autoload()
vendor/autoLoad.php
)时,所有指向的文件将被加载(需要)。您可以使用它加载将函数定义为不存在自动加载的文件。它通常也用于创建垫片(如果不存在的有条件定义的函数或类,别名类等)。。
那么有什么区别。我会说你们彼此之间无法很好地比较。与
PSR-4
相比,认为您可以使用classmap
,然后指向目录。然后,作曲家将通过dump-autoload
时间进行工作。在构建项目包进行部署时,这可能很有用。对于开发,
PSR-4
更好,因为您不需要dump-autoload
当您添加新类或重命名一个等。案例是要配置
PSR-4
。然后,当您构建用于部署的软件包时,您将使用Build-In自动加载优化。它将自动将classmap
作为psr-4
零件如生产中所示,我们不希望代码更改(classmap不得检查文件 - 系统(磁盘i/o!)不再存在,因为它在内存中)。当时使Classmap的权威成为授权,即使是倒塌,也不会再检查文件系统。使用所有这些选项以使您受益,无论是开发还是在生产中进行开发:
psr-4
。classmap
如果没有其他好选项(例如,不支持psr-4
/psr-0
的代码(检查psr) -0
适合Those are documented in
autoload
reference, in short:classmap
: point to one or more files or directories. when you executecomposer dump-autoload
(implicitly withcomposer install
andcomposer 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. 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 (compare Autoloader optimization).files
: point to one or more files. strictly speaking this is not a class autoloader (comparespl_autoload()
etc.). All files pointed to will be loaded (required) when the composer autoloader is required (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 useclassmap
and just point to the directory. Composer would then do the work atdump-autoload
time. This can be useful when building the projects package for deployment.For development,
psr-4
is better as you don't need todump-autoload
all the time when you add a new class or rename one etc.A common use-case is to have
psr-4
therefore configured. Then when you build the package for deployment, you make use of the build-in autoload optimization. It will automatically dump aclassmap
for thepsr-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). 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.classmap
if there is no other good option (e.g. code that does not supportpsr-4
/psr-0
(checkpsr-0
if it suits, it is pretty lax in Composer).files
is for file includes. You normally know when you need it. Should not be used.