PHP Autoload Manager

A friend and I have been working on different projects together. We recently decided to share one of our tool to the open source community.

Its name is the Autoload Manager.

The AutoLoad Manager is a generic autoloader that can be used with any PHP framework or library. Using the PHP tokenizer mechanism, it will parse folder(s) and discover the different classes and interfaces defined. The big advantage of using this autoloadManager is that it will allow you to implement whatever naming rules you want and may have mutliple classes in one file (if you want to have a such feature).

So basically, you don’t have anymore to implement some complex naming rules between the filename and the classname. You may organize the way you want your API (may have as many subfolders as you want, you may have multiple API folders, etc.).

What does it do ?

Well, it will basically allow you to use this tool as a generic autoload for any of your project.

How does it work ?

It will scan any given folder and find any defined PHP classes or interfaces. It will then create an hashtable that will reference what class can be found in what file. This hash table is serialized and cached in a file.

Whenever, your program or script will look for a non-existing class, the autoloadManager will look on that hash table and load the file if it exists. A fallback mechanism can be used also in a development environment that will try to rescan all the folders once more (this mechanism is usefull when you are often adding new classes to your program).

Where can I download it ?

You can browse the code and download it from github.com.

http://github.com/alfallouji/PHP-Autoload-Manager/

You can also directly get the source code from the git repository.

git clone git://github.com/alfallouji/PHP-Autoload-Manager.git

You can also download the latest stable release (ver 1.0) here :

PHP-Autoload-Manager version 1.0

Who wrote it ?

This script have been written by Bashar Al-Fallouji (bashar@alfallouji.com) and Pierrick Charron (pierrick@webstart.fr).

How can I use it ?

First, you will have simply to load the autoloadManager class into your script.

include('api/autoloadManager.php'); 

Secondly, you will have to define the path where the autoloadManager will store the file containing the serialized hash table.

// Defines the path where the file containing the autoload
// array will be stored
// Apache User must have Write Access to this folder !
DEFINE('AUTOLOAD_SAVE_PATH', sys_get_temp_dir()); 

Then, you have the two main features offered by this script.

1. Register the loadClass function:

spl_autoload_register('autoloadManager::loadClass'); 

2. Add a folder to process:

autoloadManager::addFolder('{YOUR_FOLDER_PATH}');

For instance, if your classes are found in ‘/var/www/myProject/lib’ and ‘/var/www/myProject/includes’, then you can do something like this.

autoloadManager::addFolder('/var/www/myProject/lib');
autoloadManager::addFolder('/var/www/myProject/includes');
spl_autoload_register('autoloadManager::loadClass'); 

That’s all you have to do ! You can use whatever class you want in your script now !

Can I use this code?

Yes, here is a copy of the license.

This Code is released under the GNU LGPL

Please do not change the header of the file(s).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU Lesser General Public License for more details.
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

10 Responses to PHP Autoload Manager

Avatar

Bashar’s Blog » PHP Autoload Manager - Simple, efficient and flexible

May 4th, 2009 at 12:57 am

[...] PHP Autoload Manager [...]

Avatar

anis berejeb

May 30th, 2009 at 1:11 pm

very cool ! this will help me a lot :P

Avatar

bashar

May 31st, 2009 at 7:47 am

Anis, Thanks for the comment. Glad to hear you like it.

Avatar

Apprendre a utiliser Git | Anis Berejeb

October 31st, 2009 at 8:10 pm

[...] chez github. essayons de « checkouter»  un projet. Je choisis le projet PHP AUTOLOAD MANAGER de Bashar : ?View Code SHELL1 2 3 4 5 6 7 8 9 ~$ git clone [...]

Avatar

r0b

July 22nd, 2010 at 3:04 am

Thanks for sharing that great script!
Saved me hundreds lines of includes in my project!

Avatar

Jim Osborne

November 24th, 2011 at 12:11 pm

Got it!

It did work ‘out of the box’ when I extended the test code to instantiate a class within the defined folder, i.e.

$autoloadManager = new AutoloadManager();
$autoloadManager->setSaveFile(’autoload.php’);
$autoloadManager->addFolder(’BridgeDesignPattern’);
$autoloadManager->excludeFolder(’BridgeDesignPattern/report_plugins’);
$autoloadManager->register();

$diag = new ManageDiagnostics(); // class within folder ‘BridgeDesignPattern’
$diag->setLocalArgs(10, array(10,20,30), __FILE__);
print_r($diag->getLocalArgs());

The last three lines of test code triggered the creation of the ‘SaveFile’.

Avatar

Loggy

December 9th, 2011 at 9:34 am

Jim’s clarification in particular was pretty useful although I did have to dig down into the tree to get the classes and dependencies then add them directory by directory. Essentially the SaveFile just generates an indexed array and registers it all.

I am processing a Wordpress tree from a different Wordpress program - so the classes may not be the same either because the core is a different version or different plugins etc are included. Active and inactive plugins/themes need to be included because I am using the information to modify the database and I kept coming across serialised objects that weren’t in a class.

It’s a pity the basic __autoload function assumes the class filename is the same as the class - this certainly isn’t case for most of Wordpress!

Avatar

Bashar

December 12th, 2011 at 7:12 pm

Thats right, the setSaveFile create a files containing an associative array of classname => filename.

This is usefull and recommended in a production environment, you don’t want to parse all folders each time.

On a development environment, you may want not to generate that cache file and therefore not use the setSaveFile, so whenever you add a new class or modify where files are located, your app will automatically rediscover those updates and still work.

Avatar

Angel S. Moreno

April 26th, 2013 at 2:06 pm

well, there goes wasting a couple of hours of development and a couple of days of testing. I owe you a few beers buddy. I forked the code in GitHub.

Avatar

Bashar

May 8th, 2013 at 9:05 pm

Glad that you liked it !

Comment Form

Who am I?

My name is Bashar Al-Fallouji, I am a Solution Architect at Sense.

I am particularly interested in Web applications, Open Source Development, Software Engineering, Information Architecture, Unit Testing, XP/Agile development, etc.

On this blog, you will find mostly technical articles and thoughts around PHP, OOP, OOD, Unit Testing, etc. I am also sharing a few open source tools and scripts.

  • Bashar: Glad that you liked it ! [...]
  • Angel S. Moreno: well, there goes wasting a couple of hours of development and a couple of days of testing. I owe you [...]
  • Bashar: Thats right, the setSaveFile create a files containing an associative array of classname => filen [...]
  • Loggy: Jim's clarification in particular was pretty useful although I did have to dig down into the tree to [...]
  • Jim Osborne: Got it! It did work 'out of the box' when I extended the test code to instantiate a class within [...]