Question

I understand how to register autoloaders and even how to create one, that's no issue at all. How ever the main issue is - how do you have two auto loaders running side by side for something like:

class project_one_folder_class extends project_two_folder_class{}

You'll notice that the child class belongs to a project which is reaching out and calling the parent class which is locate in a different project.

The way the projects are linked project two's classes are always seen by the auto loader, how ever project one's classes are never seen.

So the way I thought around this was to write two auto loaders and register them because php will look in on then the other. How ever php seems to be looking in only one and not the other.

how would you solve this?

Edit

Project two is parent, Project one is child. This is more expanded question then What was posted on this question.

To better expand this is my class.

class AisisCore_Loader_AutoLoader{

    protected static $_instance;

    public function get_instance(){
        if(null == self::$_instance){
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    public function reset_instance(){
        self::$_instance = null;
    }

    public function register_auto_loader(){
        spl_autoload_register(array($this, 'load_class'));
        spl_autoload_register(array($this, 'load_child_class'));
    }

    public function load_class($class){
        $path = str_replace('_', '/', $class);
        if(file_exists(get_template_directory() . '/' . $path . '.php')){
            require_once(get_template_directory() . '/' . $path . '.php');
        }
    }

    public function load_child_class($class){
        $path = str_replace('_', '/', $class);
        if(file_exists(get_stylesheet_directory() . '/' . $path . '.php')){
            require_once(get_stylesheet_directory() . '/' . $path . '.php');
        }
    }   
}

Currently this class will load anything in the parent project. It will even load parent project objects in the child project. How ever no child object can be loaded using this class as it is not found.

Those familiar with WordPress will instantly say, yes its because you have get_template_directory when you want get_stylesheet_directory How ever - Knowing this - I want to write then two auto loaders, one that will load child projects objects using get_stylesheet_directory and then one that will load parent objects via get_stylesheet_directory so that:

class project_one_folder_class extends project_two_folder_class{}

works and loads, with out error.

Was it helpful?

Solution

This is a little rough, but you actually only need one autoloader that just checks within multiple directories:

abstract class Hakre_Theme_AutoLoader
{
    public static $directories;

    public static function init()
    {
        // configure paths
        self::$directories = array(
            get_template_directory(), // current theme, parent in case of childtheme
            get_stylesheet_directory(), // current theme, child in case of childtheme
        );

        // please double check if you really need to prepend
        return spl_autoload_register('Hakre_Theme_AutoLoader::autoload', true, true);
    }

    public static function autoload($class)
    {
        $filename = str_replace('_', '/', $class) . '.php';

        foreach (self::$directories as $directory) {
            $path = $directory . '/' . $filename;
            if (is_file($path)) {
                require($path);
                return; # leave because the class has been loaded
            }
        }
    }
}


Hakre_Theme_AutoLoader::init();

## If you're unsure all worked out properly:
var_dump(Hakre_Theme_AutoLoader::$directories);

This should actually do it, however I've not tested it. See the var_dump, you can debug if the correct directories are registered or not. No need to have multiple callbacks only because you want to check in two directories.

OTHER TIPS

Well, of course you could just look up a second path when the first file_exists returns false - in the very same autoloader.

You can also register a second autoloader for the second path.

PHP docs addresses this:

If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.

Separate the autoloading process!

PHP is working like this: At some point, the code execution stumbles upon a class reference that is not yet loaded. $obj = new project_one_folder_class()

Now the registered autoloader functions are called, starting with the one added first (unless the ones added later are using the "prepend" parameter).

The first (and all subsequent) autoload function gets the name of the class that should be loaded. The function now has the task to decide

  1. Is it it's task to load this class? If not, do nothing, let the other functions try to load it.
  2. If this function is responsible to load the class, try to make up a path and filename out of the known class name, and require this file.

Step 1 is usually solved by looking if the class name to be loaded starts with a certain prefix. In your case, the autoloader responsible for loading classes starting with "project_one_" should NOT try to load any other classes starting with something else.

Step 2 takes place if the autoloader knows that it must perform the autoloading. It now transforms the class name into a relative path and filename, adds the base directory, and then requires the file.

Requiring a file that defines a class which extends another class (or implements interfaces) can then trigger another run of autoloading with any still unknown classes. In your case, the autoload process is started again with the class name "project_two_folder_class". Note that the first autoloader for "project_one_" classes will be called again with this second class name, but it must not do anything, because it does not know how to load these classes. This is up to the autoloader that knows about "project_two_" classes.

Order of registration should not matter for the different autoloaders. If it does, autoloaders misbehave.

Because it is a common pattern to simply transform any underscore characters in class names into DIRECTORY_SEPARATOR, as well as the backslashes from namespaces, add ".php" at the end, and then try to load this file from a defined base directory, the code for two of these autoloaders will be identical, and the problem is reduced to configuring only one autoload function with these two cases: Which prefix should be present, and in which base directory should the file be expected to be, provided that the class name can be transformed into a relative path to the file.

Checking in every case whether the file exists in a certain directory or not is not optimal for performance. You'll end up doing plenty of checks for files that you will be able to know you cannot load by simply looking at the class name.

And by the way: Autoloader functions do not need to be singletons. In fact, your singleton implementation is broken, and useless. Instantiate the autoloader with new, maybe pass some configuration values into the constructor, and call spl_autoload_register(), and you should be done.

You can add more than one loader, just register each function with spl_autoload_register().

procedural style:

spl_autoload_register('loader_function1'));
 // your second class location
spl_autoload_register('loader_function2'));

or in a OO style

spl_autoload_register(array($this, 'loader_function1'));
spl_autoload_register(array($this, 'loader_function2'));

However based on your question: you probably can accomplish the same thing by having a more flexible autoloader see my answer in a different question here: Class autoloader in wordpress plugin

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top