du5591 2016-03-13 19:46
浏览 41
已采纳

在codeigniter中加载模型的魔术

Guys before few days I went for interview,and interviewer asked me a question,which is still only question and I could not find its answer.

To Load a Model In Codeigniter 
we do,
$this->load->model('xyz_model');
then magically 
$this->xyz_model->function_of_xyz_model();
Above line lets us access function of 'xyz_model' 
named'function_of_xyz_model'

How it all happen,which game is played under the carpet to do all above. As I'm not well experienced in OPP so please point out the concepts(if any) used inside.

  • 写回答

1条回答 默认 最新

  • douchan7552 2016-03-13 19:56
    关注

    I imagine something like this (although it`s very robust and many validations should be done beforehand), magic methods are the key :

    class ControllerOrSimilar {
        /**
        * Loader
        */ 
        var $loader;
        public function __get($instance) {
            //retrieve static instances loaded in class Loader
            return Loader::$instances[$instance];
        }
    }
    
    class Loader {
        static $instances = array();
        public function load($modelRequested) { //instanciate in static variables to be accessed globally 
            if(!$this->$instances[$modelRequested]) {
                $this->$instances[$modelRequested] = new $modelRequested();
            }
        }
    }
    

    Although the actual implementation I think it`s different, I have worked with codeigniter but never gone in the depts of the implementation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址