User Tools

Site Tools


Adventures in autoLoading

This page is optional reading

In the course of deciding how I wanted to manage my code separate from fox, I explored several possibilities.

One of them was playing with SPL autoloading functions.

This idea was not used. Not part of the stewload mod.

But I save it here as a reminder.

It is not clear to me where will take V3.

It may be that within a year or so, they have opened up their engines to “normal” extension via OOP, introduce some namespacing, provide for autoloaders, who knows.


This is in fact the least intrusive method we found. It does away with the core hacks we needed for the stewload mod.


It has performance implications that need to be profiled, a new hook, and a way of dealing with the bootstrap process. And it always loads the original class.

We opted instead for the simple sledgehammer approach ;)

Instantiate an object

Plugin code below goes into the file at include/library/phpfox/plugin/library_phpfox_getobj_1.php

At this point in execution, the normal class file has already been loaded, and the phpfox script is about to instantiate the class into an object.

The class name tells us whether we have a service, component, or library. With that information, we register an appropriate autoloader.

The we check to see if the class exists. This fires up the autoloader. If the autoloader finds a script in the custom folder, it is required (loaded).

So if our loader found noting in the custom folder, fox will instantiate the standard class.

Otherwise it will use the name of the class we just loaded.

    (c) 2014
    This replaces instantiation, not loading.
    So no matter what happens, the original phpfox codebase is intact and in place.


    // What kind of object do we need to instantiate ?
        $sLoader =  'StewLoader::loadLib';

    // Set an auto loader for the kind of class we expect to load

    // Prefix the class name
    $sCustomClass = 'custom_' . $sClass ;
    if ( class_exists($sCustomClass))  // has the side effect of invoking the loader we just registered
        // Yes we have a custom class and it is now loaded
        $sClass = $sCustomClass; // replace the class name and let the fox take it from here.   

    // Remove loader

It was a nice thought. While the code itself is simple and contained, we discard it as overly complex for this point in time. There are too many possible “gotchas” that may surface as V3 morphs.

products/stewload/custom.txt · Last modified: 2014/06/30 11:58 by steward