PHP 7 interview question · Most asked
How does `spl_autoload_register()` work, and why was `__autoload()` deprecated?
Summary
spl_autoload_register() adds a callback to a stack of autoloaders that PHP calls when an unknown class is referenced; __autoload() allowed only a single global function, so libraries conflicted. It was deprecated in 7.2 and removed in 8.0.
Longer Answer
Composer registers its own loader via spl_autoload_register. Multiple loaders can coexist and are tried in order. Autoloaders should not throw when a class isn't found, so the next loader gets a chance.
Key Points
- Stack of autoloaders
- Composer uses it under the hood
__autoloaddeprecated 7.2, removed 8.0
More most-asked PHP 7 questions
- What are typed properties in PHP 7.4?
- How do return type declarations work in PHP 7?
- What were the headline improvements of PHP 7.0 over PHP 5.6?
- Explain scalar type declarations and coercive vs strict mode in PHP 7.
- Explain the `Throwable`, `Error` and `Exception` hierarchy introduced in PHP 7.
- What are nullable types in PHP 7.1?
- What does the null coalescing operator `??` do, and how is it different from `isset()` with a ternary?
- What are arrow functions in PHP 7.4 and how do they differ from closures?