Non-destructive spl_autoload_register

I’m wanting to use the dynamic loading of classes via spl_autoload_register in a wordpress plugin i’m developing but the problem is it can’t interfere with pre-existing implementations of this feature. In my initial attempt:

// register an autoloader function for template classes
spl_autoload_register ( 'template_autoloader' );

function template_autoloader ( $class ) {
    include LG_FE_DIR . "/includes/chart_templates/class.{$class}.php";
}

seems to work in loading my own classes but at the same time causes tons of errors from other plugins that apparently were also using the spl_autoload_register feature.

Read More

Any ideas?

Related posts

Leave a Reply

1 comment

  1. I believe I have a reasonable solution now but open to other’s opinions. My solution is to have the template loader function test for the existence of the file first. I have also elected to continue to hook into the “init” hook so static includes / requires are loaded first (or at least most of them). The function now looks like this:

    add_action ( 'init' , 'class_loader' );
    
    function class_loader () {
        // register an autoloader function for template classes
        spl_autoload_register ( 'template_autoloader' );
    }
    
    function template_autoloader ( $class ) {
    if ( file_exists ( LG_FE_DIR . "/includes/chart_templates/class.{$class}.php" ) ) 
        include LG_FE_DIR . "/includes/chart_templates/class.{$class}.php";
    
    }