Proposal for "Autoloader"

» Metadata » Status
  • Status: Proposed
» Description
This is a general purpose Autoloader. It's intended to be used outside of PEAR in any

It finds class (and interface) definitions automatically in the file system.
The Autoloader doesn't follow any convention. Any file might contain any amount of
class (and interface) definitions.

Searching for files is done by an ordered list of file candidates. Files which sound
more like the class name are prefered. This might lead to memory problems, as the
complete directory tree must be traversed. For that case the FileIterator might be
replaced against a classic recursive FileIterator:

Autoloader::getRegisteredAutoloader()->setFileIterator(new FileIterator_Simple());

An Autoloader has one class path. Class definitions are only searched recursively in
this class path. If you have several disjunct class paths you have to register for
each class path an Autoloader:

$autoloader1 = new Autoloader($classPath1);

$autoloader2 = new Autoloader($classPath2);

Creating an Autoloader without any class path will make the directory of the caller
to the class path. This creates equivalent Autoloader objects:

$autoloader1 = new Autoloader(__DIR__);
$autoloader2 = new Autoloader();

The most common use case is creating an Autoloader without any class path and
registering it. This is done by simply requiring the file
"autoloader/Autoloader.php". An enduser might have this in his application entry

require __DIR__ . "/autoloader/Autoloader.php";

That's all. Now Autoloading is ready to work.

Once a file was found it's stored in an index. So next time searches don't consume
any filesystem iteration. The index is per default a compressed serialized hashtable.
Other indexes are implemented and might be changed:

Autoloader::getRegisteredAutoloader()->setIndex(new AutoloaderIndex_PDO($pdo));

Namespaces are supported. The Autoloader works also before PHP 5.3.

The Autoloader supports the optional class constructor
public static function classConstructor(). This is comparable to the java static block.
A class constructor is called once per class when the class definition is loaded.

If you think building the index dynamically is a stupid idea, building a complete
index with relative paths in advance is supported. It's done by adding the
AutoloaderIndexFilter_RelativePath to the index and calling buildIndex() on the
Autoloader object. Although tests exists, it's still experimental. But it's there.

The license of the packages was GPL 3. Your form doesn't offer that license. If GPL 3
is a problem I will change the license to one of the supported licenses.
» Dependencies » Links
  • PHP >= 5
» Timeline » Changelog
  • First Draft: 2010-05-27
  • Proposal: 2010-05-27