dsi36131 2013-03-20 20:38
浏览 61
已采纳

PHP中的自动加载器 - 一次运行两个

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.

  • 写回答

4条回答 默认 最新

  • doujunchi1238 2013-03-20 21:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛