在 codeigniter 中加载模型背后的魔法
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经验不多,所以请指出里面使用的概念(如果有的话)。
我想象这样的事情(虽然它非常强大并且应该事先进行许多验证),magic methods 是关键:
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();
}
}
}
尽管我认为实际实现有所不同,但我使用过 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经验不多,所以请指出里面使用的概念(如果有的话)。
我想象这样的事情(虽然它非常强大并且应该事先进行许多验证),magic methods 是关键:
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();
}
}
}
尽管我认为实际实现有所不同,但我使用过 codeigniter,但从未参与过实现部门