Php codeigniter加载模型背后的魔力
前几天我去面试,面试官问了我一个问题,这仍然是唯一的问题,我找不到答案Php codeigniter加载模型背后的魔力,php,oop,codeigniter-2,Php,Oop,Codeigniter 2,前几天我去面试,面试官问了我一个问题,这仍然是唯一的问题,我找不到答案 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_xy
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();
}
}
}
虽然我认为实际的实现是不同的,但我使用过codeigniter,但从未参与过实现的各个部分
load
实例化一个对象并将其分配给$this->xyz\u model
。