Loading classes automatically in PHP



Before you can use a class, you need to include it in your script, usually with require_once. The more classes you use, the more include commands you need. On a big project, this becomes time consuming, so PHP provides a way of automating the process with __autoload(). Like __construct(), the name starts with two underscores.

Using __autoload() depends on two things, namely:

1) Each class must be defined in a separate file.

2) You must adopt a consistent naming convention for both classes, and the file structure they are stored in.


If you give each class the same name as the file where it is defined, __autoload() is very simple. Just add the following code at the beginning of the main file of your application:

function __autoload($class)
{
require_once $class . '.php';
}


This concatenates the .php file name extension onto the class name and calls require_once. So, if you have a class called MyClass and store it in MyClass.php, PHP will automatically search your include_path for MyClass.php the first time that you create an instance of MyClass.

The naming convention combines the folder name with the file name. So Ch2_Product is in Ch2/Product.php. This involves just a little extra coding inside __autoload() to create the correct file path like this:

function __autoload($class)
{
$parts = explode('_', $class);
$path = implode(DIRECTORY_SEPARATOR, $parts);
require_once $path . '.php';
}


This breaks the class name at each underscore into an array and rebuilds it into the path name using the PHP DIRECTORY_SEPARATOR constant. Finally, it concatenates the .php file name extension onto the class name and includes the class file.

Attempting to redefine a function causes a fatal error, so it is a good idea to put the __autoload() definition in an external file and include it using require_once. Needless to say, it must be included before your script attempts to instantiate any objects.

Both examples of __autoload() assume that your classes are in your PHP include_path. If they are outside the include_path, you need to define the full path in the __autoload() function. For example, on my Windows testing machine, the OopSolutions site is located in C:\htdocs\OopSolutions. So, I need to alter the second example like this:

function __autoload($class)
{
$parts = explode('_', $class);
$path = 'C:\htdocs\OopSolutions\\' . implode(DIRECTORY_SEPARATOR, ĺ
$parts);
require_once $path . '.php';
}


The double backslash is required at the end of the path to prevent PHP from interpreting the closing quote as an escaped character. On a Mac or Linux, the path is written with forward slashes, so the final slash should not be doubled. Note that you need to use a physical path to the folder containing the classes, not a URL.

With __autoload() there is no need to use require_once to include a class file before using it. PHP loads the class definition on the fly. It does this only once for each class, so the impact on performance is normally minimal.