Invokes registered SPL autoloaders for a class name.
What It Does
Composer PSR-4 autoloading supplies a project-level class map Manually calling the autoloader rarely adds value; normal class lookup already triggers the loader.
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_register(static function (string $class): void {
echo "Looking for {$class}\n";
});
spl_autoload_call('Example\Task');
Example 2: Another Case
Handles an alternative input or use case.
<?php
$attempted = [];
$loader = static function (string $class) use (&$attempted): void { $attempted[] = $class; };
spl_autoload_register($loader);
spl_autoload_call('UnknownClass');
print_r($attempted);
spl_autoload_unregister($loader);
Practical Example
Puts the function into a larger practical task.
<?php
$loader = static function (string $class): void {
$file = __DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php';
if (is_file($file)) require_once $file;
};
spl_autoload_register($loader);
// PHP automatically calls registered loaders when the class is referenced.
var_dump(class_exists('App\Order'));
For production, apply the compatibility and edge-case limits described below.
Important Behavior and Edge Cases
Manually calling the autoloader rarely adds value; normal class lookup already triggers the loader.
Comparison with Related Functions and Tools
| Subject | Key difference |
|---|---|
| spl_autoload_call() | Composer PSR-4 autoloading supplies a project-level class map |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Calling spl_autoload_call() without its required input or supported extension | Check the extension, supply valid arguments, and handle false or exceptions according to the function. |
| Assuming spl_autoload_call() guarantees a valid or safe image | Inspect the return and avoid exposing debug output to website visitors. |
| Confusing the operation with a related API | Composer PSR-4 autoloading supplies a project-level class map |
Key Takeaways
- Invokes registered SPL autoloaders for a class name.
- Manually calling the autoloader rarely adds value; normal class lookup already triggers the loader.
- 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.