Content · Web Development · PHP

PHP Autoloading with spl_autoload_register()

Register an autoload callback to locate PHP classes when first referenced; use strict namespace-to-path mapping.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Register an autoload callback to locate PHP classes when first referenced; use strict namespace-to-path mapping.

What It Does


A custom loader needs a namespace boundary and predictable path mapping. Namespace, class name and case-sensitive path must match the Composer mapping.

Requirements and Compatibility


Use a supported PHP runtime and install any third-party tool or extension explicitly shown in the examples. Commands should be run from the project root unless stated otherwise.

Syntax, Inputs and Results


The examples separate configuration, the smallest working operation and a realistic follow-up task. Adapt file paths and dependency versions to your own project.

Example 1: Basic Use


A custom loader needs a namespace boundary and predictable path mapping.

<?php
spl_autoload_register(static function (string $class): void {
    if (!str_starts_with($class, 'Legacy\\')) return;
    $relative = substr($class, strlen('Legacy\\'));
    $path = __DIR__ . '/src/' . str_replace('\\', '/', $relative) . '.php';
    if (is_file($path)) require $path;
});

Example 2: Another Case


Namespace, class name and case-sensitive path must match the Composer mapping.

<?php
// File: src/Domain/Invoice.php
namespace Legacy\Domain;
final class Invoice { public function total(): int { return 250; } }

Practical Example


Loading vendor/autoload.php registers the Composer loader before class use.

<?php
require __DIR__ . '/vendor/autoload.php';
$invoice = new Legacy\Domain\Invoice();
echo $invoice->total();

For production, apply the compatibility and edge-case limits described below.

Important Behavior and Edge Cases


Do not let untrusted names choose arbitrary local file paths.

Comparison with Related Functions and Tools


SubjectKey difference
Composer autoloading vs manual requireComposer maps namespaces to source files; require loads one file explicitly at the call site.

Common Beginner Mistakes


MistakeCorrect approach
Mismatching namespace and directory namesMake case and path separators match the PSR-4 map, especially on Linux.
Skipping the prerequisite of this specific workflowInstall or configure the named tool and confirm the example can actually run.
Ignoring edge conditionsDo not let untrusted names choose arbitrary local file paths.

Key Takeaways


  • Register an autoload callback to locate PHP classes when first referenced; use strict namespace-to-path mapping.
  • Do not let untrusted names choose arbitrary local file paths.
  • Keep application configuration, source code and validation tests under version control.

Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.

Share this resourceSend this page to someone who may find it useful.