views:

47

answers:

1

spl_autoload_register can do this kind of job,but I don't understand how is that kind of job done?

spl_autoload_register(array('Doctrine', 'autoload'));
+3  A: 

The basic idea is that you don't have to write include/require instructions anymore : whenever you're trying to use a non-defined class, PHP will call the autoloader.

The job of the autoloader, then, is to determine which file should be loaded, and include it, so the class becomes defined.

PHP can then use that class, as if you were the one who wrote the include instruction, that has in fact been executed in the autoloading function.


The "trick" is that the autoloading function :

  • only receives the name of the class
  • has to determine which file to load -- i.e. which file contains that class.

This is the reason for naming convention, such as the PEAR one, which says that class such as Project_SubProject_Component_Name are mapped to files such as Project/SubProject/Component/Name.php -- i.e. '_' in the class names are replaces by slashes (directories, subdirectories) on the filesystem.


For instance, if you take a look at the Doctrine_Core::autoload method, which is the one that will be called as an autoloader in your case, it contains this portion of code (after dealing with some specific cases) :

$class = self::getPath() 
            . DIRECTORY_SEPARATOR . 
            str_replace('_', DIRECTORY_SEPARATOR, $className) 
            . '.php';
if (file_exists($class)) {
    require $class;
    return true;
}
return false;

Which means the class name's is mapped to the filesystem, replacing '_' by '/', and adding a final .php to the file name.

For instance, if you're trying to use the Doctrine_Query_Filter_Chain class, and it is not known by PHP, the Doctrine_Core::autoload function will be called ; it'll determine that the file that should be loaded is Doctrine/Query/Filter/Chain.php ; and as that file exists, it'll be included -- which means PHP now "knows" the Doctrine_Query_Filter_Chain class.

Pascal MARTIN
What if there are many classes registered,will all of them be executed or only the one matching `Doctrine`?
If you have several autoloading functions registered with `spl_autoload_register`, you'll have a "queue" of autoloading functions. PHP will *(quoting http://www.php.net/spl_autoload_register )* "run through each of them in the order they are defined" ;;; which means the first funtion will be called ; if the class still doesn't exist, the second will be called ; and so on -- independanty of the class name.
Pascal MARTIN
To add a bit of information to my previous comment : the `"Doctrine"` string you are using only means that the autoloading function that you are registering is the static method `autoload` in the `Doctrine` class *(see "callback" here : http://www.php.net/manual/en/language.pseudo-types.php )* ;;; it doesn't say anything about what classes that autoloader will be able to autoload.
Pascal MARTIN
I followed that link,seems `spl_autoload_register` doesn't accept an array as parameter,but why the above still works?
`spl_autoload_register` accepts a *"callback"* as its first parameter ; and an array containing a classname as first element, and a static method name as second element is a valid callback -- see the link I posted in my second comment.
Pascal MARTIN
Great answer!+1
Thanks :-) Have fun !
Pascal MARTIN