2011-05-15 68 views
2

當寫的CodeIgniter應用我的控制器動作往往開始的幾行如下:笨延遲加載庫/模型/等

$this->load->model('abc_model'); 
    $this->load->library('ijk'); 

然後(只是爲了完整性),他們使用方法如下:

$this->abc_model->fetch_123(); 
    $this->ijk->do_something(); 

對於延伸MY_Controller是否會有任何錯誤,以便以下可能?

$this->model('zbc_model')->fetch_stuff(); 
    $this->library('ijk')->do_something(); 

優點:

  1. 的類不被加載,直到他們實際使用
  2. 就不需要使用config/autoload.php
  3. 稍微乾淨的代碼自動加載任何類(可以說)

缺點:

  1. 一個額外的方法調用,每次訪問(通常只是返回雖然已經加載實例)
  2. 稍微混亂的代碼(可以說)

回答

2

使用菲爾鱘魚的technique,將它添加到您的應用程序/配置/配置.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); 
} 
}