问题描述
我知道您可以使用 PSR 标准来定位文件,或者告诉作曲家一个目录来扫描类.文档建议使用 PSR-4 标准.还有一个选项让作曲家创建一个优化的自动加载器,它基本上生成一个完整的类图.那么,如果加载的最佳方式是使用类映射,为什么还要使用 PSR-4?
I understand that you can use either a PSR standard to locate files, or tell composer a directory to scan for classes. The documentation recommends using the PSR-4 standard. There is also an option for composer to create an optimized autoloader, which basically generates a full classmap. So why should one use PSR-4 at all if the best way to load is with a classmap?
保留目录结构对我来说很有意义,因为无论如何这是一种很好的组织方式.然而,似乎合乎逻辑的选择是在开发机器上使用 PSR-4 加载,然后在生产环境中使用类映射.这样,您不必每次创建新类时都重新构建类映射,但生产环境会在部署过程中创建一个完整的类映射,而无需额外调用
It makes sense to me to keep the directory structure, since that is a good way to organize anyway. However, it seems like the logical option would be to use PSR-4 loading on development machines, and then classmap for the production environment. That way, you don't have to rebuild your classmap every time you create a new class, but the production environment creates a complete one as a part of the deployment process without an additional call to
./composer.phar dump-autoload -o
推荐答案
因为它更实用.
在生产中,您可以使用类映射(使用 composer dumpautoload -o
),因为您不会添加任何新类,但在开发环境中,拥有 PSR-0 提供的灵活性很有趣或 PSR-4(即添加新类时无需执行任何操作).
In production, you can use a classmap (with composer dumpautoload -o
) because you won't add any new class, but in dev environment it's interesting to have the flexibility provided by PSR-0 or PSR-4 (i.e. nothing to do when adding new classes).
更新:你也可以使用composer install -o
,更简单.
Update: you can also use composer install -o
, it's simpler.
这篇关于如果 classmap 实际上更快,为什么要在 Composer 中使用 PSR-0 或 PSR-4 自动加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!