PHP Autoloader features
Namespaces
Since PHP 5.3 exists namespaces. The Autoloader supports them completly. PHP Autoloader also finds classes with its namespaces if using multiple namespaces in the same file. It doesn't matter if you use the bracketed syntax or the simple syntax.
Index based class search
In order to find a class definition quickly, PHP Autoloader maintains an index. The index is updated automatically if a class definition is deleted, moved or found new. These index structures are supported:
- AutoloaderIndex_SerializedHashtable_GZ Compressed serialized hashtable (default)
- AutoloaderIndex_SerializedHashtable Serialized hashtable
- AutoloaderIndex_PDO PDO
- AutoloaderIndex_IniFile Hashtable from a ini file
- AutoloaderIndex_CSV Hashtable from a CSV file
- AutoloaderIndex_PHPArrayCode Hashtable in PHP-Code - This implementation was ment for debuging purposes. It should not be used productivly. It might give the possibility to execute arbitrary code in your application's environment.
- AutoloaderIndex_Memcache Shared key/value storage Memcached
Optimized file search
If a class definition was not found in the index, the class definition will be searched uniquely in the filesystem. Files which ends on .php or .inc and are similar to the class name are preferred. After those candidate files all other files are scanned.
Class constructor
After requiring a class definition PHP Autoloader tries to call the optional existing
class constructor
__static()
classConstructor().
PHP doesn't know a class constructor. A class constructor is called only once unlike the
object constructor __construct().
It resides with the definition
public static function classConstructor()
in the static class context.
This could be useful for initialization of class members:
<?php
class Singleton
{
/**
* @var Singleton
*/
private static $_instance;
public static function classConstructor()
{
self::$_instance = new self();
}
/**
* @return Singleton
*/
public static function getInstance()
{
return self::$_instance;
}
private function __construct()
{
}
private function __clone()
{
}
}
Multiple usage
Multiple autoloaders can be used at the same time. PHP autoloader respects the existance of other autoloaders. It doesn't matter if the other autoloaders are alien, this autoloader or an instance of this autoloader from another place in the filesystem.