Class Autoloading

Autoloading provides a hook to allow the loading of a class, the first time PHP requires it. This is a more flexible way to handle the class definitions than the traditional inclusions, which requires good planning.

Autoloading requires each class, trait, interface or enumeration to be stored in a distinct file. This is the most popular way, yet autoloading allows any other strategy of storage, as long as the class may be loaded based only on its name.

Autoloading relies on the spl_autoload_register() function. Prior to PHP 8.0, it was possible to define the __autoload() method, and this function has been deprecated since PHP 7.2.

Autoloading does not work for global constants and functions. Those still require a specific inclusion mechanism.

In modern PHP, not using class autoloading is considered a bad practice.

<?php

    spl_autoload_register(function ($class_name) {
        include $class_name . '.php';
    });

    $obj  = new MyClass1();
    $obj2 = new MyClass2();

?>

Documentation

See also Use autoloading and namespaces in PHP, Autoloading Performance – Avoid These 5 Mistakes! and Understanding Autoloading in PHP: How to Implement and Use It Efficiently.

Related : Autowiring, Definition, Call, __autoload, Class Hoisting, Inclusions, Standard PHP Library (SPL), Existence, Hoisting, Library, Order Of Execution, include

Added in PHP 5.0