The benefit of __autoload() and its successor spl_autoload_register()
Posted on September 1, 2012 by Alfredo Scaini
PHP has a great magic method called __autoload(). Now, before you say that anything magic is dangerous and should be steered clear of, hear me out.
Yes, there are some things in the past that were bad ideas (anyone remember magic_quotes?), but today’s magic methods in PHP really help a developer in a number of ways – __autoload is one of them.
How it Works
__autoload() is called whenever you try to create an object from a class that your system can’t find; either because you’ve forgotten to include it into your system; because you spelled the name incorrectly; or, because you’re purposely trying to conserve server memory (hint, hint).
When PHP can’t find this class within your system, it will look for an __autoload() function within the application. If found, it will comb through the code you’ve added into the function to try and find the class, then create the object from that class.
The best way to explain __autoload is with an example. The __autoload() function below would sit in a file that is always called in your application:
/*
* __autoload()
*
* This magic function will be called when your system is unable
* to find the class that you are trying to call.
*
* CLASS_DIR is a constant within your configuration file that
* contains the directory path to your classes.
*
* Note that this example expects your classes to follow a
* naming convention. Use naming conventions throughout your app -
* it's important.
*
* @param $class_name string The name given to the class
* @return (nothing)
*/
function __autoload($class_name) {
$class_path = CLASS_DIR . 'class.' . $class_name . 'inc.php';
if (file_exists($class_path)) {
include($class_path);
}
} // end of __autoload()
In your application you would have a class called Foo within a file called, “class.foo.inc.php” within a central class folder (defined in your configuration file … never hard code paths outside of your config file unless they are relative paths).
When you try to create a Foo object, and you’ve not loaded in the class.foo.inc.php file, __autoload() will kick in and execute the code that you added it it.
<?php
require_once('config.php');
require_once('file-where-your-autoload-fn-is-located.php');
$foo = new Foo();
?>
If class.foo.inc.php exists within the class directory, __autoload will include it into your application and your $foo object will be of class Foo().
How is this Good?
The main benefit of this is that you are better able to manage the memory your application uses.
Imagine that you have 100 class files, but only some of them are used some of the times. Instead of loading 100 50KB class files (which amount to 5MB), you only load up those that are needed for the particular script that is being run by a user.
This might not seem like a lot, but if you have no control over how much memory your server can assign to your app, then every MB counts.
Another side benefit is that you don’t have 100 include_once(‘class.[name].inc.php’) in your files … ick.
Update: spl_autoload_register()
__autoload is being phased out of PHP and php.net recommends that you start using spl_autoload_register()
/*
* class_loader()
*
* This called when your system is unable
* to find the class that you are trying to call.
*
* CLASS_DIR is a constant within your configuration file that
* contains the directory path to your classes.
*
* Note that this example expects your classes to follow a
* naming convention. Use naming conventions throughout your app -
* it's important.
*
* @param $class_name string The name given to the class
* @return (nothing)
*/
function class_loader($class_name) {
$class_path = CLASS_DIR . 'class.' . $class_name . 'inc.php';
if (file_exists($class_path)) {
include($class_path);
}
} // end of class_loader()
// Load your class_loader() file so that it's recognized by
PHP as your auto-loader.
spl_autoload_register('class_loader');