Search code examples
phpwordpressautoloadsplautoloader

Autoloaders in PHP - two running at a time


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.


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.