沃梦达 / 编程问答 / php问题 / 正文

使用子命名空间自动加载我的类 (Composer.json)

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

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

问题描述

我已将以下内容添加到我的 composer.json 文件中.这很好用,但我有一长串想要包含的子命名空间(例如 Apple、Orange、Lemon、Pear、Banana... 等).

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) 我必须指出每个子命名空间还是有快捷方式,例如."纯\*": "纯"

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) 包含自定义自动加载文件是否更好:

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.

如果这个示例包是您创建的唯一一个,并且它是唯一一个使用 Pure 作为命名空间的包,如果多个子目录中较长前缀的数量过多,请继续使用.但是,这假设您正在使用的世界上任何其他包都应该避免使用相同的命名空间做同样的事情.

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 将能够在所有可用目录中搜索子命名空间,即如果您有两个包,其中一个说 Pure 可以在 pure/src/Pure,而另一个说 Purecode/stuff 中,Composer 将尝试在其中一个中找到一个类 PureSomethingClass这些目录首先,然后必须尝试第二个,如果它没有找到它.Composer 会记住 pure/src/Pure/Something 目录是否存在,如果必须加载该命名空间中的第二个类,则避免在那里查找以 PureSomething 开头的任何内容.

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 PureSomethingClass 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 PureSomething 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:

不,不惜一切代价避免它.您不会从中获得任何好处,因为文件总是必须被加载,并且它是占用一些内存的重复代码 - 您已经拥有 Composer 的自动加载器.如果您有不符合 PSR-4 或 PSR-0 的代码,则可以使用类映射.对于新代码:仅使用 PSR-4!

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!

此外,您的自定义自动加载器无法通过 Composer 进行优化.尽管应该衡量任何优化的有效性(阅读我对此的详细回答:如果 classmap 实际上更快,为什么要在 composer 中使用 PSR-0 或 PSR-4 自动加载?),使用您自己的自动加载器会完全阻止你的代码被优化,如果它是有益的.

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

本文标题为:使用子命名空间自动加载我的类 (Composer.json)

基础教程推荐