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
  • __autoload deprecated 7.2, removed 8.0

More most-asked PHP 7 questions

← All PHP 7 questions