问题描述
我已将以下内容添加到我的 composer.json
文件中。这工作正常,但我有一个长列表的子命名空间(例如苹果,橙,柠檬,梨,香蕉...等),我想包括。
1)我必须指明每个子命名空间或有快捷方式。 Pure\\ *:pure
composer.json:
autoload:{
psr-4:{
Pure \\:pure,
Pure \\Apple\\:pure / src / Pure / Apple,
Pure\\Orange\\ \\\:pure / src / Pure / Orange,
Pure\\Lemon\\:pure / src / Pure / Lemon
}
}
2)最好包含自定义自动加载文件:
composer.json:
:{
files:[
pure / src / Pure / autoload.php
]
}
autoload.php:
spl_autoload_register(function($ class){
// etc ...
}
解决方案声明自动加载时,应使用尽可能长或合理的前缀。
如果此示例包是只有一个你正在创建,它是唯一使用
Pure
作为命名空间,如果几个子目录中较长前缀的数量太高,那么去。但是,假设您使用的任何其他包在世界上应该避免使用相同的命名空间做同样的事情。
Composer将能够搜索子命名空间在所有可用的目录中,即如果你有两个包,并且一个说
Pure
是在pure / src / Pure
,另一个表示Pure
位于code / stuff
中,Composer将尝试查找类Pure\Something\Class
在这些目录之一,然后必须尝试第二个如果没有找到它。 Composer将记住是否存在pure / src / Pure / Something
目录,并避免查找以Pure\Something
但是从代码组织的角度来看,一个包应该只提供一组定义的命名空间,而没有其他包应该在同一命名空间中提供类。您可以不小心将同一个类添加到两个包中,并得到有趣的问题,如果两个文件不同,可能很难调试。
不,不惜一切代价。你不会从中获得任何好处,因为文件总是必须加载,它是占用一些内存的重复代码 - 你已经有自动加载器从Composer。如果您的代码不符合PSR-4或PSR-0,则可以使用类映射。对于新代码:仅使用PSR-4!
此外,您的自定义自动加载器无法通过Composer进行优化。虽然任何优化应该衡量的有效性(请阅读我的详细答案:),使用自己的自动加载器完全防止您的代码被优化,如果它是有益的。
I've added the following to my
composer.json
file. This works fine but I have a long list of sub-namespaces (eg. Apple, Orange, Lemon, Pear, Banana... etc) that I want to include.1) Do I have to indicate each sub-namespace or is there a shortcut eg.
"Pure\\*": "pure"
composer.json:
"autoload": { "psr-4": { "Pure\\": "pure", "Pure\\Apple\\": "pure/src/Pure/Apple", "Pure\\Orange\\": "pure/src/Pure/Orange", "Pure\\Lemon\\": "pure/src/Pure/Lemon" } }
2) Is it better to include a custom autoload file instead:
composer.json:
"autoload": { "files": [ "pure/src/Pure/autoload.php" ] }
autoload.php:
spl_autoload_register(function ($class) { //etc... }
解决方案When declaring the autoloading, you should use the longest possible or reasonable prefix.
If this example package is the only one you are ever creating, and it is the only one using
Pure
as the namespace, go with that if the number of longer prefixes in several subdirectories is too high. However, this assumes that any other package in the world that you are using should avoid doing the same thing with the same namespace.Composer will be able to search for sub namespaces in all available directories, i.e. if you have two packages, and one says
Pure
is to be found inpure/src/Pure
, and the other saysPure
is incode/stuff
, Composer will try to find a classPure\Something\Class
in one of these directories first, then has to try the second one if it does not find it. Composer will remember whether thepure/src/Pure/Something
directory exists and avoid looking for anything starting withPure\Something
there if a second class in that namespace has to be loaded.But from a code organization standpoint, one package should only ever provide a defined set of namespaces, and no other package should provide classes in the same namespace. You could accidentially add the same class into two packages and get interesting problems that may be hard to debug if the two files are different.
No, avoid it at all cost. You won't get any benefit from this because the file always has to be loaded, and it is duplicate code that occupies some memory - you already have the autoloader from Composer. If you have code that does not conform to PSR-4 or PSR-0, you can use a classmap. For new code: Use PSR-4 only!
Also, your custom autoloader cannot be optimized by Composer. Although any optimization should be measured for effectiveness (read my detailed answer on this: Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?), using your own autoloader will entirely prevent your code from being optimized if it would be beneficial.
这篇关于使用子命名空间自动加载我的类(Composer.json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!