i dont really get the docs for spl_autoload
bool spl_autoload_register ([ callback $autoload_function ] )
from my understanding, it will
What say is correct. It's called when an unknown class is instantiated. You then get the chance to include/require the necessary file.
When the function resides in a class, you have to pass in an array. In this case it's a static function because they pass in the Doctrine class instead of an instance of the Doctrine class.
Your entire statement is correct.
spl_autoload_register
allows you to register multiple autoload functions. In the case that you try to create or use an object of a class that has not been loaded into the PHP environment, PHP will run all your autoload functions looking for that class.
The Doctrine example you give is using what is called a callback to register a method inside of a class. A callback is simply an array containing the class name (for static methods) or an instance of that class (for non-static methods), and the method name.
spl_autoloader_register registers a callback function/method, that will be called when your code is trying to use a not-known class.
A callback function can be described in several ways :
'my_function'
array('MyClass', 'myMethod')
array($myObject, 'myMethod')
In the case of Doctrine, it seems to be the second solution : if a class that PHP doesn't know is used, the autoloader will call Doctrine::autoload
, passing it the class name as a parameter.
so i can also register many autoload functions?
Yes, you can, with spl_autoload_register :
If there must be multiple autoload functions,
spl_autoload_register()
allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined.
But we don't generally define one autoloading function/method for each class we have ; that would be quite inefficient, I suppose.
Instead, we use the class-name, which is received by the autoloading function, to decide which file should be included.
For instance, an autoload function might look like this :
function my_autoloader($className)
{
require LIBRARY_PATH . '/' . $className . '.php';
}
The trick being that if your files are named after the classes they contain, it immediatly becomes much easier ;-)
This is why classes are often named using the PEAR convention : My_Class_Name
maps to the file My/Class/Name.php