Autoloaders in PHP - two running at a time

泪湿孤枕 提交于 2019-12-06 09:05:43

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!