PHP Auto Load Includes

I have been recently working with a new contractor and he mentioned after looking at my code that I should think about auto loading the php includes using the __autoload method. At first this sounded great but I soon saw the limitations in this magic method.

The method is called when a class is instantiated but is not defined as a last ditch attempt to find it. Writing a basic function to load in classes that have not been included yet is easy but I wanted a system to include everything.

So I have created a class to handle this. It basically loops through all the files in a directory you specify and then reads all the file paths into an array. Then loops through the array trying to include each file. It also handles classes that use inheritance and registers the super class first.

After it has successfully included all your php files it saves the list of classes to a cached file so it does not have to spend time rebuilding the list. It will however rebuild the list if a class is called for but is not defined.