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.
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