downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

spl_classes> <spl_autoload_unregister
Last updated: Fri, 13 Nov 2009

view this page in

spl_autoload

(PHP 5 >= 5.1.2)

spl_autoload__autoload() のデフォルト実装

説明

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

この関数は、__autoload() のデフォルト実装として使用されることを意図しています。 ほかに何も指定されておらず、autoload_register() がパラメータなしでコールされた場合には、 その後の __autoload() のコール時にはこの関数が使用されます。

パラメータ

class_name

file_extensions

デフォルトでは、クラス名を小文字にして .inc および .php を拡張子につけたファイル名のファイルが存在するかどうかを すべてのインクルードパスから探します。

返り値

値を返しません。



add a note add a note User Contributed Notes
spl_autoload
simast at gmail dot com
08-Aug-2009 05: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 02: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

spl_classes> <spl_autoload_unregister
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites