Content · Web Development · PHP

PHP spl_autoload() Function: Usage and Examples

Legacy default SPL loader that tries class filenames with registered extensions.

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

Legacy default SPL loader that tries class filenames with registered extensions.

What It Does


spl_autoload_register() allows a predictable custom class loader Prefer an explicit class map or Composer PSR-4 autoloading; filename resolution depends on include_path and extensions.

Requirements and Compatibility


Available in the PHP runtime; use PHP 8 or later for the shown typed, modern examples.

Syntax, Inputs and Results


The arguments shown below are concrete values for this function; their types and ranges matter. The example checks return data rather than assuming that every operation succeeds.

Example 1: Basic Use


Demonstrates the primary behavior.

<?php
spl_autoload_extensions('.php');
// With a matching class file on the configured include_path:
spl_autoload('Report');

Example 2: Another Case


Handles an alternative input or use case.

<?php
$previous = spl_autoload_extensions();
spl_autoload_extensions('.class.php,.php');
if (is_file(__DIR__ . '/Report.class.php')) {
    set_include_path(__DIR__ . PATH_SEPARATOR . get_include_path());
    spl_autoload('Report');
}
echo spl_autoload_extensions(), "\n";
spl_autoload_extensions($previous);

Practical Example


Puts the function into a larger practical task.

<?php
// For modern code, prefer a predictable class-to-path loader:
spl_autoload_register(static function (string $class): void {
    $file = __DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php';
    if (is_file($file)) require $file;
});

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

Important Behavior and Edge Cases


Prefer an explicit class map or Composer PSR-4 autoloading; filename resolution depends on include_path and extensions.

Comparison with Related Functions and Tools


SubjectKey difference
spl_autoload()spl_autoload_register() allows a predictable custom class loader

Common Beginner Mistakes


MistakeCorrect approach
Calling spl_autoload() without its required input or supported extensionCheck the extension, supply valid arguments, and handle false or exceptions according to the function.
Assuming spl_autoload() guarantees a valid or safe imageInspect the return and avoid exposing debug output to website visitors.
Confusing the operation with a related APIspl_autoload_register() allows a predictable custom class loader

Key Takeaways


  • Legacy default SPL loader that tries class filenames with registered extensions.
  • Prefer an explicit class map or Composer PSR-4 autoloading; filename resolution depends on include_path and extensions.
  • Choose the companion API described in the comparison table when the job differs.

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.