使用子命名空间自动加载我的类(Composer.json) [英] Autoload My Classes with Sub-Namespaces (Composer.json)

查看:634
本文介绍了使用子命名空间自动加载我的类(Composer.json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将以下内容添加到我的 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进行优化。虽然任何优化应该衡量的有效性(请阅读我的详细答案:为什么使用PSR-0或PSR-4自动加载如果classmap实际上更快?),使用自己的自动加载器完全防止您的代码被优化,如果它是有益的。


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...
}

解决方案

Do I have to indicate each sub-namespace or is there a shortcut

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 in pure/src/Pure, and the other says Pure is in code/stuff, Composer will try to find a class Pure\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 the pure/src/Pure/Something directory exists and avoid looking for anything starting with Pure\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.

Is it better to include a custom autoload file instead:

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆