简体   繁体   中英

Autoload My Classes with Sub-Namespaces (Composer.json)

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, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM