Overview of the different available loaders

What is a loader

The idea is that files are not loaded from some specially defined path using include() or require(). Instead a loader is used to load the needed files, and the loader knows from where to load the files.

This way one can change from loading all the state classes from one directory to loading them from PHP's include path just by changing the type of loader that's used when loading those classes. This is not limited to PHP files or class files, this can as well be used to load config files. So that by using one loader the config files would be loaded from a specified directory and by using another loader they would be loaded from e.g. an installed PEAR package's data directory, and using a third loader the config files would be loaded from a database.

Please note that not all functionality related to loading other files than PHP files containing classes is implemented yet. Patches are very welcome.

Currently two loaders are available, a file system based one and one that uses PHP's include path.

File system loader (Ismo_Core_Loaders_FileSystem)

This loader tries to load files from a specified directory in the file system. An optional prefix and postfix might be added to the requested file when it's searched from the directory.

Example:

            $loader =& new Ismo_Core_Loaders_FileSystem();
            $loader->setPath('./states/');
            $loader->setPrefix('MyStateClasses_');
        

This creates a loader which will try to load the files from the directory named states in the current working directory. Then a class is requested using the getInstance() method the prefix MyStateClasses_ will be added. So if

            $classInstance =& $loader->getInstance('Menu');
        

is called. The loader will search for a file named MyStateClasses_Menu.php in the directory states. If such a file is found, an instance of a class with the name MyStateClasses_Menu is created and returned.

One can change the postfix added to the file name and the extension, see the API docs for details (it's obvious how it works).

Include path loader (Ismo_Core_Loaders_IncludePath)

PHP's include path doesn't really work exactly like Java's classpath (if your familiar with it) so this class can't be as powerful as I would like it to be. Maybe things will get better with PHP5 but for now one has to manually add all files this loader should be able to load. That is done like this:

            $loader =& new Ismo_Core_Loaders_IncludePath();
            $loader->addFile('main', 'Ismo_Example_States_Main',
                'Ismo/Example/States/Main.php');
            $loader->addFile('edit', 'Ismo_Example_States_Edit',
                'Ismo/Example/Other_States/Edit.php');
        

After this,

            $instance =& $loader->getInstance('main');
        

can be called to get an instance of the class Ismo_Example_States_Main. The loader knows it should include the file Ismo/Example/States/Main.php and make an instance of a class named Ismo_Example_States_Main.

This can also be used to load for example config files from an installed PEAR package's data directory among other things.