Why would I use spl_autoload_register instead of include or require for classes and functions?
php
Solution
The advantages of an autoloading function or class are:
- If you have a lot of classes, you don't need to manually include them. Take Zend Framework as an example: You don't want to load all of those classes manually. With an autoloader, just create a new class and start using it in all of your (autoloader-enabled) files.
- Only files that are actually used in the request are loaded, potentially saving resources. If you only use 10 out of 50 files, why load the other 40?
- It forces you to have a sensible directory layout (after all, you'll need some rules so your autoloader can find files).
- It can (but not by definition) be faster than including manually. The benchmark in the linked article shows that if you use about three quarters of all of your available classes on your page, you'll 'break even'. Even then, autoloading makes your life so much easier that even if it was significantly slower than manually including, you should still do it.
Problem
I don't understand what `spl_autoload_register` or `autoload` is doing. Why wouldn't I just use include or require? I didn't see a clear cut answer.