Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

Autoloading classes in PHP

How PHP autoloading works: spl_autoload_register(), writing a PSR-4 style loader, when the loader runs, class_exists() and why Composer is used.

Autoloading lets PHP load a class file only at the moment the class is first used, so you do not need a long list of require lines. When code mentions an unknown class (new Invoice, Invoice::create(), a type check with instanceof does not trigger it), PHP calls every registered autoloader with the fully qualified class name until one defines the class.

You register a loader with spl_autoload_register(). The usual convention, PSR-4, maps a namespace prefix to a directory: App\Models\User becomes src/Models/User.php. In real projects Composer generates this loader for you.

PHP
spl_autoload_register(function (string $class): void {
    // turn $class into a file path and require it if it exists
});

Try it

PHP A tiny PSR-4 autoloader

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP class_exists() and the autoload flag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
spl_autoload_register(?callable $callback = null, bool $throw = true, bool $prepend = false): bool
spl_autoload_unregister(callable $callback): bool
spl_autoload_functions(): array
Name Description
$callback Receives the class name as a string (with namespace, no leading backslash). It should require the file; its return value is ignored.
$throw Always treated as true since PHP 8.0; an exception thrown by the loader propagates.
$prepend Put this loader at the front of the queue instead of the end.

Return value: true on success, false on failure.

Usage notes

  • When it runs. The loader is called for new, static calls and constants, extends/implements and class_exists(). Writing Foo::class or $x instanceof Foo does not load Foo: the first is just a string, the second is simply false for an unknown class.
  • Only once per class. After a class is defined PHP never asks again, so a loader does not have to check for loaded classes.
  • Several loaders can coexist (your own, Composer's, a framework's). They run in registration order until the class exists; keep yours cheap and make it return silently when the file is not found.
  • Case. Class names are case-insensitive in PHP but files are not on Linux. Keep file names exactly matching class names.
  • PSR-4 maps namespace prefix to base directory, class name to file name, one class per file. Composer's "autoload": {"psr-4": {"App\\": "src/"}} does this and caches a class map for speed (composer dump-autoload -o).
  • Functions and constants are not autoloaded; use Composer's files autoload entry or require.
  • __autoload() was removed in PHP 8.0. Always use spl_autoload_register().
  • Errors. If no loader defines the class, PHP throws Error: Class "X" not found.
  • Security. Never build the path from user input without checking the class name against your own prefixes.