Die Flagge des Marasek

Dekostreifen

Deutsch

Current Texts Comic Imprint Calendar Search PHP-Classes Container-Wizard main.s21

Categories

Class Library
Computers
Gaming
Global Politics
Programming
Society
Weblog
World Outlook
{{login}}

paran: Autoloader

Permalink
Previous: "Kauf ist billiger als Miete"Next: paran: Callback
Assigned keywords: Programmieren

The first in the series is the Autoloader, which is usually the second line of code within one of my projects - with the first requiring the Autoloader.
The Autoloader handles all the tedious task of doing require prior to using classes. And as it is more complex than a simple "look for file at" => "load file", it allows for elaborated directory layouts with well laid out structures. Basically, the Autoloader recursively goes through a directory and builds an index of all files ending with .php. It then assumes that the file Class.php will contain the class Class. Once load is called, the class is looked up within the index and loaded.
To reflect that more directories exist within a project, the loader can be given several directories:

<?php
$load 
= new Loader("classes");
$load->addRepository("models");
$load->addRepository("project");
spl_autoload_register(array($load"load"));
?>

Note that Loader does not register an autoload handler by himself - it leaves that to you, which gives you more control over the process. Also note that the indexes are merged together - if a class occurs within classes and project, the class within project will be loaded.

Concerning the directory structure, the Loader supports complex structures with subdirectories, allowing you to build a structure best representing your work. It assumes that a file <name>.php contains the class <name>, but allows for certain special names as well:

AbstractClass.abstract.php
MyInterface.interface.php
MyClass.class.php
SomeFunctions.function.php

While the first three allow you organize your classes more clearly, the fourth file will be included instantly. This should be used for standalone functions, which cannot be autoloaded on demand. But since you're a good OO programmer, you won't need those, rrright? Also note that "class" is optional.

Now recursively indexing whole directory trees might become cumbersome at some point and there is little reason why the same process should be started over and over again. To improve performance, you can store the results of a run within a simple index file (which is basically a CVS file containing classes and directories):

<?php
$load 
= new Loader("classes");
$load->useIndex("index.txt");
$load->addRepository("models");
$load->addRepository("project");
spl_autoload_register(array($load"load"));
?>

Now an index is written to index.txt first time or whenever a class is requested which cannot be found. Performance can be increased further by vetoing certain directories:

<?php
$load 
= new Loader("classes");
$load->addVeto(".svn");
$load->useIndex("index.txt");
$load->addRepository("models");
$load->addRepository("project");
spl_autoload_register(array($load"load"));
?>

Now all directories matching ".svn" (exact match) will be ignored during any index run.

Dieser Text ist Teil der Serie Paran in Action

paran: Autoloader
paran: Callback
paran: Basic
paran: EPDO

Comments

Please note: comments posted won't be visible immediately, as they will be checked for harmful content.

* Title  
* Nickname  
* Comment