SPL 函数
在线手册:中文 英文
PHP手册

spl_autoload

(PHP 5 >= 5.1.2)

spl_autoload__autoload()函数的默认实现

说明

void spl_autoload ( string $class_name [, string $file_extensions ] )

本函数提供了__autoload()的一个默认实现。如果不使用任何参数调用 autoload_register() 函数,则以后在进行 __autoload() 调用时会自动使用此函数。

参数

class_name

file_extensions

在默认情况下,本函数先将类名转换成小写,再在小写的类名后加上 .inc 或 .php 的扩展名作为文件名,然后在所有的包含路径(include paths)中检查是否存在该文件。

返回值

没有返回值。


SPL 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: __autoload()函数的默认实现

用户评论:

Luke Scott (20-Apr-2011 03:03)

If you want to make the best use out of autoload with an APC cache don't use spl_autoload. It uses relative paths and thus will perform a stat even with apc.stat=0 (either that, or it doesn't work at all).

Instead make a custom function and use require/include with an absolute path (register it with spl_autoload_register).

Do NOT use *_once functions or a relative path. This will fail harder than spl_autoload.

Also avoid using file_exists and is_file. This will also perform a stat.

Why are stats bad? Because they access the file system. PHP does have a stat cache that helps, but it defeats the purpose of apc.stat = 0.

It's also good to keep in mind that it's good to keep your custom autoload function simple. This is my Loader class:

<?php

class Loader
{   
    public static function
registerAutoload()
    {
        return
spl_autoload_register(array(__CLASS__, 'includeClass'));
    }
   
    public static function
unregisterAutoload()
    {
        return
spl_autoload_unregister(array(__CLASS__, 'includeClass'));
    }
   
    public static function
includeClass($class)
    {
        require(
PATH . '/' . strtr($class, '_\\', '//') . '.php');
    }
}

?>

Also want to point out that APC does an optimization with require/include (not *_once) with relative paths if require/include is done in the global scope (and isn't conditional). So it would be a good idea to explicitly include files you know you're going to use on every request (but don't use *_once). You could, for example, add a "registerProfiledAutoload" to the above class and keep track of what you're including to help you determine what you could explicitly include (during development, not production). The key is try not to make heavy use out of autoload.

If you must use relative paths and don't care about having to lower-case your file-names then spl_autoload works great.

daniel (06-Jul-2010 03:58)

Note this function will LOWERCASE the class names its looking for, dont be confused when it cant find Foo_Bar.php

also, unlike most other autoloader code snippets, this function DOES NOT translate underscores to slashes.

class Foo_Bar {}
will load foo_bar.php and will not try to load foo/bar.php

You can get around this with
spl_autoload_register(function($class) { return spl_autoload(str_replace('_', '/', $class));});

simast at gmail dot com (08-Aug-2009 06:22)

Note, that the default autoload implementation is written in C land and is always slightly faster then your native PHP one.

Here is a trick to use default implementation with any configuration:

<?php

   
// Your custom class dir
   
define('CLASS_DIR', 'class/')

   
// Add your class dir to include path
   
set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR);

   
// You can use this trick to make autoloader look for commonly used "My.class.php" type filenames
   
spl_autoload_extensions('.class.php');

   
// Use default autoload implementation
   
spl_autoload_register();
?>

This also works with namespaces out of the box. So you can write code like "use My\Name\Object" and it will map to "class/My/Name/Object.class.php" file path!

safak_ozpinar at NOSPAM dot yahoo dot com (26-Sep-2007 03:17)

Note that, the orders of file extensions is important for performance. You should make the priority of your favourite file extension higest or use only one extension for your class files. Check out this example:

Some class files:

ClassA.php
<?php class ClassA { var $val = 'Hello from class "ClassA"'; } ?>
ClassB.php
<?php class ClassB { var $val = 'Hello from class "ClassB"'; } ?>
ClassC.php
<?php class ClassC { var $val = 'Hello from class "ClassC"'; } ?>
ClassD.php
<?php class ClassD { var $val = 'Hello from class "ClassD"'; } ?>
ClassE.php
<?php class ClassE { var $val = 'Hello from class "ClassE"'; } ?>

1. Simple:
<?php
// default priority: .inc .php
for($n=65; $n<70; $n++) {
   
$className = 'Class'.chr($n);
   
spl_autoload($className);
   
$ins = new $className;
    echo
$ins->val.'<br>';
}
// 4.2 miliseconds
?>

2. Change priority:
<?php
spl_autoload_extensions
('.php,.inc');
// new priority: .php .inc
for($n=65; $n<70; $n++) {
   
$className = 'Class'.chr($n);
   
spl_autoload($className);
   
$ins = new $className;
    echo
$ins->val.'<br>';
}
// 1.4 miliseconds
?>

Or you can use this simple function that runs a bit faster for the extensions with lower priority :)
<?php
function my_autoload($className, $extList='.inc,.php') {
   
$ext = explode(',',$extList);
    foreach(
$ext as $x) {
       
$fname = $className.$x;
        if(@
file_exists($fname)) {
            require_once(
$fname);
            return
true;
        }
    }
    return
false;
}

for(
$n=65; $n<70; $n++) {
   
$className = 'Class'.chr($n);
   
my_autoload($className);
   
$ins = new $className;
    echo
$ins->val.'<br>';
}
// 2.6 miliseconds
?>
---
Safak Ozpinar - Istanbul University, Computer Engineering