When writing CodeIgniter applications my controller actions tend to begin with a few lines as below:
$this->load->model('abc_model');
$this->load->library('ijk');
And then (just for completeness) they're used as follows:
$this->abc_model->fetch_123();
$this->ijk->do_something();
Would there be anything too wrong about extending MY_Controller
so that the following was possible?
$this->model('zbc_model')->fetch_stuff();
$this->library('ijk')->do_something();
Pros:
- Classes aren't loaded until they're actually used
- Wouldn't need to auto-load any classes using
config/autoload.php
- Slightly cleaner code (arguably)
Cons:
- An extra method call for every access (generally just returning the already loaded instance though)
- Slightly messier code (arguably)
Use Phil Sturgeon's technique, add this to your application/config/config.php
/*
| -------------------------------------------------------------------
| Native Auto-load
| -------------------------------------------------------------------
|
| Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
@include_once( APPPATH . 'core/'. $class . EXT );
}
}
来源:https://stackoverflow.com/questions/6011271/codeigniter-lazy-loading-libraries-models-etc