2011-01-07 47 views
0

我是codeigniter的新手。我想知道使用$ CI = & get_instance();我想知道使用$ CI = & get_instance();我想知道使用$ CI = & get_instance();CodeIgniter Basic

用於錯誤記錄或全局配置變量。

在此先感謝。

回答

9

從笨手冊:

$ CI = & get_instance();

一旦你指定的對象到 變量,您將使用的變量,而不是 這$:

$CI =&get_instance(); 

$CI->load->helper('url'); 
$CI->load->library('session'); 
$CI->config->item('base_url'); etc. 

注意:您會注意到上面的 get_instance()函數被 傳遞通過引用:

$CI =& get_instance(); 

這是非常重要的。通過指定 引用,您可以使用原始CodeIgniter對象 而不是創建它的副本。

另外,請注意:如果您正在運行 PHP 4它通常是最好的,以避免內 類的構造函數調用 get_instance()。 PHP 4具有 引用應用程序構造函數 中的CI超 對象的問題,因爲直到 類完全實例化爲止對象不存在。

鏈接: http://codeigniter.com/user_guide/general/creating_libraries.html

+0

感謝它幫助了我很多。非常感謝你 – 2011-01-07 09:40:57

0

只有延伸是CI_Controller,模型類,查看可以使用

$this->load->library('something'); 
$this->load->helper('something');//etc 

自定義類不能使用上面的代碼。 使用上述功能,在您的自定義類,您必須使用

$CI=&get instance(); 
$CI->load->library('something'); 
$CI->load->helper('something'); 

例如,在你的自定義類

// this following code will not work 
Class Car 
{ 
    $this->load->library('something'); 
    $this->load->helper('something'); 
} 


//this will work 
Class Car 
{ 
    $CI=&get_instance(); 
    $CI->load->library('something'); 
    $CI->load->helper('something'); 
} 
// Here $CI is a variable.