在编码点火器中加载模型背后的魔力


Magic behind loading model in codeigniter

伙计们几天前我去面试,面试官问了我一个问题,这仍然只是问题,我找不到答案。

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'

这一切是如何发生的,哪个游戏在地毯下玩,以完成上述所有操作。由于我在OPP方面没有丰富的经验,因此请指出内部使用的概念(如果有的话)。

我想像这样的事情(尽管它非常健壮并且应该事先进行许多验证),魔术方法是关键:

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();
        }
    }
}

虽然实际的实现我认为是不同的,但我使用过编码点火器,但从未进入实施部门