Simple PHP Autoloader alternative Though I have heard about the magic method __autload, I still prefer to use this code:

function autoload($array)
{
    foreach ($array as $value) 
    {
        switch ($value) 
        {
            case 'database' :
                include ROOT_DIR.'/core/database.php';
                break ;
            case 'user' :
                include ROOT_DIR.'/core/user_class.php';
                break ;
            case 'error_handler' :
                include ROOT_DIR.'/core/error_class.php';
                break ;
            case 'template' :
                include ROOT_DIR.'/template/rain.tpl.class.php';
                break ;
            case 'email' :
                include ROOT_DIR.'/core/email_class.php';
                break ;
        }
    }
}


I'm not exactly sure if this is a good option.  Should I switch to the magic method __autoload or keep using my simple switch case?  If you propose that I need to use the magic method instead, is it due to a performance-wise factor?  If not, why are you suggesting it?
It depends if this is for OOP or procedural code.  I am assuming OOP as you use class in some of your file names.

Procedural

If this is for procedural code then it should be renamed to load as there is nothing auto about it (see the OOP review).

OOP

If this is for OOP then it is not a good option. spl_autoload_register to register a standard autoload function is better.

Your Solution


There is a hardcoded entry that needs to be maintained for each class that you want to include.  This does not scale well with hundreds or thousands of entries to look through.
If the value is not recognized you ignore it silently because you don't have a default entry in your switch statement.
You have to manually autoload the classes that you want (this takes the auto out of autoload).


Advantages of spl_autoload_register


spl_autoload_register is a true automatic loader.

// We just need one call to register an autoload function, this registers the
// autoloading for the lifetime of the program.
spl_autoload_register();
$exampleObject = new Example_Object;

// In the file autoloaded for class Example_Object:
class Example_Object
{
    // Type hinting works to load the class for Another_Object.
    public exampleMethod(Another_Object $another)
    {
        // Autoloading works here too.
        $lastObject = new Last_Object;
    }
}



The autoloader only needs to be registered once and continues to load files for classes right throughout the program.


A stack of autoload functions can be provided allowing for flexibility in the logic used to load classes.
Because files are loaded lazily only what is required is loaded no matter how much you edit the other files.
Using logic to load your class files helps you to organize them logically.  It makes your logic simpler if you name your class files logically. This helps when you have hundreds or thousands of files that have been named in a consistent manner.


What not to use an autoloader for

An autoloader is for loading the file needed to define a class. You should not use it to load functions or templates.  These should still be included manually.