Would the second autoload method I have below be better performance?
<?php
// regular __autoload function
function __autoload( $name )
{
include( $class . '.php' );
}
// SPL autoload
spl_autoload_register("MyClass::Autoloader");
class MyClass
{
public static function Autoloader($class)
{
// list of classes and their location
$classes = array();
$classes['spooncookie'] = 'cookie/cookie.php';
$classes['spoondatabase'] = 'database/database.php';
$classes['spoondatagrid'] = 'datagrid/datagrid.php';
$classes['spoondatagridcolumn'] = 'datagrid/column.php';
$classes['ispoondatagridpaging'] = 'datagrid/paging.php';
// ....list of all the other class files.......
if(isset($classes[$class])){
include( $classes[$class] );
}
}
}
?>
The best way to get an accurate idea of which will give the best performance for your own usage is to test each of them. As a general rule, different methods of autoloading will be optimal in different scenarios, factors include: how many files/classes you have in total, how deep the directory structure is, and how many classes you will load on average per request.
The key point behind using an autoloader isn’t to make your code run more quickly, but to improve how quickly you can write the code in the first place.
For production code I really would recommend going with a well used autoloader from a framework instead of rolling your own, at least to start with. and then maybe modifying it later if you know its the bottleneck and can be improved.
As a side note, have you seen this?
http://weierophinney.net/matthew/archives/245-Autoloading-Benchmarks.html
EDIT: