CKTIS / PHP
spl_autoload_register()
Register a loader that receives an unresolved class name without a leading backslash.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
spl_autoload_register(?callable $callback = null, bool $throw = true, bool $prepend = false): boolRegister a loader that receives an unresolved class name without a leading backslash. PHP tries loaders in registration order until the declaration appears. The prepend option moves a new loader to the front. Return values from individual loaders do not declare a class. Since PHP 8 throw=false is ignored with E_NOTICE. A loader that throws interrupts the queue.
Signature
spl_autoload_register(?callable $callback = null, bool $throw = true, bool $prepend = false): bool
Example output
[false,true]
notice
stop false
Examples
<?php
class LocalBadge {}
$loader=function($name) { if ($name==="RemoteBadge") { class_alias(LocalBadge::class,$name); } };
spl_autoload_register($loader);
echo json_encode([class_exists("RemoteBadge",false),new RemoteBadge() instanceof LocalBadge]);
spl_autoload_unregister($loader);
$called=false; set_error_handler(function($level){ echo "\n",($level===E_NOTICE ? "notice" : "unexpected"); return true; });
spl_autoload_register(function($name){throw new RuntimeException("stop");},false); restore_error_handler();
spl_autoload_register(function($name) use (&$called) { $called=true; });
try {class_exists("NotYetLoaded");} catch (RuntimeException $e) {echo "\n",$e->getMessage()," ",json_encode($called); }
Result
[false,true] notice stop false