spl_autoload_register()

spl_autoload_register() — Register given function as __autoload() implementation.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.1.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Register a function with the spl provided __autoload queue. If the queue is not yet activated it will be activated.

Signature

spl_autoload_register(?callable $callback = null, bool $throw = true, bool $prepend = false): bool callback(string $class): void

Parameters

ParameterTypeWhat it takes
$callback?callableThe autoload function being registered. If null, then the default implementation of spl_autoload() will be registered.
$throwboolThis parameter specifies whether spl_autoload_register() should throw exceptions when the callback cannot be registered.
$prependboolIf true, spl_autoload_register() will prepend the autoloader on the autoload queue instead of appending it.

Return value

Returns true on success or false on failure.

Example

A runnable snippet:

// function __autoload($class) { // include 'classes/' . $class . '.class.php'; // } function my_autoloader($class) { include 'classes/' . $class . '.class.php'; } spl_autoload_register('my_autoloader'); // Or, using an anonymous function spl_autoload_register(function ($class) { include 'classes/' . $class . '.class.php'; });

Changelog

VersionChange
8.0.0callback is now nullable.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page spl_autoload_register() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.