Search code examples
composer-phpautoloader

Why the return value in autoload.php of composer?


I am focusing on the very short, generated file vendor/autoload.php and its final statement:

return ComposerAutoloaderInitXXXX::getLoader();

Looking at the logic of the code, when executed from an HTTP request, I could not see the need of a return value. I have removed the return in the final statement, only kept

ComposerAutoloaderInitXXXX::getLoader();

and tested with a simple "HELLO WORLD" web application and it worked the same way.

Perhaps that in some other applications of vendor/autoload.php we might need a return value.

What would be an example of such an application?


Solution

  • From composer's documentation:

    Including that file will also return the autoloader instance, so you can store the return value of the include call in a variable and add more namespaces. This can be useful for autoloading classes in a test suite, for example.

    $loader = require __DIR__ . '/vendor/autoload.php';
    $loader->addPsr4('Acme\\Test\\', __DIR__);