php spl_autoload_register vs __autoload?

__autoload is generally considered obsolete. It only allows for a single autoloader. Generally you should only use __autoload if you’re using a version of PHP without support for spl_autload_register.

spl_autoload_register allows several autoloaders to be registered which will be run through in turn until a matching class/interface/trait is found and loaded, or until all autoloading options have been exhausted. This means that if you’re using framework code or other third party libraries that implement their own autoloaders you don’t have to worry about yours causing conflicts.

UPDATE:

__autoload is now officially deprecated as of PHP 7.2.0, which means it’s now on the chopping block. If you want your code to be compatible with future versions of PHP you definitely should not use __autoload

Leave a Comment