A Brief History of PHP Autoloading

What is autoloading in PHP? Autoloading is a functionality to help developers including PHP classes automatically. In the ancient times, we need to include all the files one by one in the bootstrap file. This olden style may also slow down the initialization of an application.

Olden style


require "class_1.php";

require "class_2.php";


PHP Autoloading Classes

PHP provides us a convenient way to handle this scenario. The spl_autoload_register function. __autoload has been DEPRECATED as of PHP 7.2.0. it's preferred to use the spl_autoload_register() function

bool spl_autoload_register ([ callable autoload_function [, bool throw = true [, bool$prepend = false ]]] )

The spl_autoload_register solution


Composer Autoloading

Composer is a powerful tool that can help us to both manage and autoload our own classes as well as others.

The autoload option in composer.json

"autoload": {
    "psr-4": {
        "AParse\": "src/",
        "Tests\": "tests/"
    "files": [

New created files or directories should be added here, or an error message will be thrown out when you try to instantiate a class or call a function.

The advantages of composer

Namespace alias

The advantage of composer autoload is the namespace don't need reflecting the directory path. The autoload_classmap.php returns an array of namespace and file referencing pairs is located at vendor/composer/. You can put a class at root/services/MyService.php with a namespace \ProjectName\MyService. If you are using spl_autoload_register directly, it's not easy to apply namespace alias.

Dependency management

Composer is not only a package manager but also a dependency manager. The installation of a new package and the dependent packages is automatic and quite.

This the brief history of PHP autoloading in my professional time. Welcome to the Modern Coding Life.

References: PSR-4 Autoloading, PHP autoloading