PHP Autoloader Classindex
PHP-Autoloader stores the location of a found class definition in an index. A rescan of the filesystem is not necessary.
The index needs to do these tasks:
- Store
- Search
- Delete
Storing happens only when a class doesn't exist in the index. The Autoloader searches the class in the filesystem and stores the location in the index.
Deleting happens only when a location of a class definition changed. The Autoloader fetches the assumed location of a class definition from the index. There it doesn't find an appropriate class definition, so it deletes the false location in the index, searches in the filesystem and stores the right location in the index.
Those two use cases, storing and deleting, happen disproportionately infrequently. They are no further discussion worth for choosing an index. Searching is the most often use case and crucial for choosing the index implementation:
- AutoloaderIndex_File
-
A hashtable is stored in a file in one of the following formats:
- AutoloaderIndex_SerializedHashtable
- The hashtable is serialized.
- AutoloaderIndex_SerializedHashtable_GZ
- The hashtable is serialized like AutoloaderIndex_SerializedHashtable does. Additionally the file is gzip compressed. The compression should reduce IO to a minimum.
- AutoloaderIndex_IniFile
- The hashtable is stored in a configuration file.
- AutoloaderIndex_CSV
- The hashtable is stored in a CSV file.
- AutoloaderIndex_PHPArrayCode
- The hashtable is stored in plain 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_PDO
- An arbitrary PDO object is queried with Standard-SQL.
The object method Autoloader::setIndex(AutoloaderIndex $index)
sets the index of an Autoloader:
<?php
require dirname(__FILE__) . '/autoloader/Autoloader.php';
// Choose an AutoloaderIndex implementation
$index = new AutoloaderIndex_IniFile();
/* fetch the automatically registered Autoloader for
* the class path of this file
*/
$autoloader = Autoloader::getRegisteredAutoloader();
// The index of the Autoloader is set to $index.
$autoloader->setIndex($index);
If you don't do this a AutoloaderIndex_SerializedHashtable_GZ index will be used.
AutoloaderIndex_File
| Action | Complexity |
|---|---|
| Initialization | O(n) |
| Query | O(1) |
For the AutoloaderIndex_File the initialization and memory usage is critical. Initialization needs to read the complete file unserialized into memory. This depends linearly on the index size. For the complexity the choice of the concrete AutoloaderIndex_File implementation doesn't matter.
Therefore queries on the index are independend on the index size constantly fast. Preferably use this index if a huge part of the index is queried.
AutoloaderIndex_PDO
| Action | Complexity |
|---|---|
| Initialization | O(1) |
| Query | O(log(n)) |
It's hard to make a generalized statement for the AutoloaderIndex_PDO. Its qualities depend heavily on the PDO implementation. You could imagine a PDO implementation which uses a serialized hash table. This implementation would have identical qualities as AutoloaderIndex_SerializedHashtable.
But you can assume that mostly a DBS with a filesystem optimized index (like a B-tree) will be used. In the web environment you would mostly use SQLite, MySQL, PostgreSQL or MSSQLServer (list of PDO implementations).
Such an index has no initialization effort. Therefore the complexity of a query depends (usually logarithmic) on the index size.
This implementation should be used on a huge index with a sufficient small usage.