I’ve been doing some PHP development lately, and I just came across a handy little magic function for PHP 5 called __autoload. If you’re like me and have each class definition stored in its own file, then you can use this function to automatically include class definition files whenever they’re needed. For example, consider the following index.php file:

function __autoload($className){
	require_once $_SERVER['DOCUMENT_ROOT'] . 'includes/' 
            . $className . '.inc.php';
}
 
$x = new Foo();

Since, in the above example, there is no definition for the Foo class, the __autoload magic function will automatically look to include a file called ‘Foo.inc.php’ in the directory ‘includes’ off your web root.

Pretty handy if you ask me.