User Tools

Site Tools


products:stewload:custom

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 phpfox.org 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.

PRO

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

CON

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 stewfoxdev.com
    This replaces instantiation, not loading.
    So no matter what happens, the original phpfox codebase is intact and in place.
*/

if(defined('STEWLOAD_CUSTOM'))
{

    // What kind of object do we need to instantiate ?
    if(strstr($sClass,'_component_'))
    {
        $sLoader='StewLoader::loadComponent';
    }
    elseif(strstr($sClass,'_service_'))
    {
        $sLoader='StewLoader::loadService';
    }
    else
    {
        $sLoader =  'StewLoader::loadLib';
    }

    // Set an auto loader for the kind of class we expect to load
    spl_autoload_register($sLoader);

    // 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
    spl_autoload_unregister($sLoader);
}

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