Removes a specific autoloader callback from the SPL autoload stack.
What It Does
Composer PSR-4 autoloading supplies a project-level class map You must pass the same callback instance used for registration; removing a different closure does nothing.
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
$loader = static function (string $class): void {};
spl_autoload_register($loader);
var_dump(spl_autoload_unregister($loader));
Example 2: Another Case
Handles an alternative input or use case.
<?php
$callback = static function (string $class): void {};
spl_autoload_register($callback);
$before = count(spl_autoload_functions());
spl_autoload_unregister($callback);
echo $before - count(spl_autoload_functions());
Practical Example
Puts the function into a larger practical task.
<?php
function temporaryLoader(string $class): void { error_log($class); }
spl_autoload_register('temporaryLoader');
// Remove it when the temporary loading scope ends.
spl_autoload_unregister('temporaryLoader');
For production, apply the compatibility and edge-case limits described below.
Important Behavior and Edge Cases
You must pass the same callback instance used for registration; removing a different closure does nothing.
Comparison with Related Functions and Tools
| Subject | Key difference |
|---|---|
| spl_autoload_unregister() | Composer PSR-4 autoloading supplies a project-level class map |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Calling spl_autoload_unregister() 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_unregister() 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
- Removes a specific autoloader callback from the SPL autoload stack.
- You must pass the same callback instance used for registration; removing a different closure does nothing.
- 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.