php - Model/library lazy load in CodeIgniter -
i have in codeigniter:
$this->load->model('test_model'); $this->test_model->....
i want just:
$this->test_model->...
i don't wanna autoload models, want load model on demand. how can add "lazy load" logic ci_controller
? __get()
? logic should add?
thanks in advance!
ps please don't confuse question codeigniter lazy-loading libraries/models/etc - have different targets.
current solution
update ci_controller::__construct()
(path system/core/controller/
) like
foreach (is_loaded() $var => $class) { $this->$var = ''; $this->$var =& load_class($class); } $this->load = ''; $this->load =& load_class('loader', 'core');
then add new method ci_controller
class
public function &__get($name) { //code here @twisted1919's answer }
the below seems not work in ci(matter fact, magic methods won't work), i'll leave here refrence though others.
well, in specific case, should (in my_controller):
public function __get($name) { if (!empty($this->$name) && $this->$name instanceof ci_model) { return $this->$name; } if (is_file(apppath.'models/'.$name.'.php')) { $this->load->model($name); return $this->$name; } }
l.e, second try:
public function __get($name) { if (isset($this->$name) && $this->$name instanceof ci_model) { return $this->$name; } if (is_file($modelfile = apppath.'models/'.$name.'.php')) { require_once ($modelfile); return $this->$name = new $name(); } }
but also, need watch helpers, libraries, etc.
Comments
Post a Comment