PHP Composer Autoload difference between classmap and files

Viewed 60

What is the difference on autoload of property classmap vs. files.

They both makes the same thing and work the same way?

{   
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Computer\\Automobile\\": "automobile/"
        },
        "classmap": [
            "classes/automobile.php"
        ],
        "files": [
            "files/automobile.php"
        ]
    }
}

I know the PSR-4 standard requires of you a strict filesystem structure based on namespaces. Say you have an app directory in the src directory with App namespace, then all sub-namespaces will mirror sub-directories and class names will be the same as file names without the .php extension.

1 Answers

Those are documented in autoload reference, in short:

  • classmap: point to one or more files or directories. when you execute composer dump-autoload (implicitly with composer install and composer update as well), Composer will scan these path(s) for PHP files for classes/interfaces/traits/enums and creates a mapping for those fully qualified names to the files pathnames that contain them. When the autoloader kicks in, that classmap is checked and then the file required if in the map. You can make the classmap authorative, it is highly versatile (compare Autoloader optimization).

  • files: point to one or more files. strictly speaking this is not a class autoloader (compare spl_autoload() etc.). All files pointed to will be loaded (required) when the composer autoloader is required (vendor/autoload.php). You can use it to load files that define functions as no autoloading exists for that. It is also often used to create shims (conditionally defined functions or classes if they don't exist, alias classes etc.).

So what is the difference. I'd say those you can't compare very well to each other. Compared with psr-4 thought you could instead use classmap and just point to the directory. Composer would then do the work at dump-autoload time. This can be useful when building the projects package for deployment.

For development, psr-4 is better as you don't need to dump-autoload all the time when you add a new class or rename one etc.

A common use-case is to have psr-4 therefore configured. Then when you build the package for deployment, you make use of the build-in autoload optimization. It will automatically dump a classmap for the psr-4 parts as in production we don't expect the code to change any longer (and the classmap must not check the file-system any longer (disk i/o!) as it is in memory). Making the classmap authorative then even means, the file-system won't be checked any longer even as a fall-back.

Use all of these options to your benefit, be it development or later on in production:

  • Go with psr-4 by default.
  • Use classmap if there is no other good option (e.g. code that does not support psr-4 / psr-0 (check psr-0 if it suits, it is pretty lax in Composer).
  • files is for file includes. You normally know when you need it. Should not be used.
  • Then in your build configuration do the autoloader optimization.
Related