Heim  >  Artikel  >  Backend-Entwicklung  >  php $CI =& get_instance();,phpci_PHP教程

php $CI =& get_instance();,phpci_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:04:37987Durchsuche

php $CI =& get_instance();,phpci

初学php 看有人这样写,$CI =& get_instance();

要你自定义的类库中访问CodeIgniter的原始资源,你必须使用 get_instance() 函数.这个函数返回一个CodeIgniter super object.

一般来说在你的控制器函数中你可以通过 $this 调用任何可用的CodeIgniter函数:

$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
etc.
$this, 只直接作用在你自己的控制器,模型和视图中.当你在自定义类中想使用CodeIgniter原始类时,你可以这样做:

首先,定义CodeIgniter对象赋给一个变量:

$CI =& get_instance();

一旦定义某个对象为一个变量,你就可以使用那个变量名 取代 $this:

$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() .php4在引用位于构造函数中的CI super object时存在问题,因为对象只有在类完全实例化后才存在.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/965015.htmlTechArticlephp $CI = get_instance();,phpci 初学php 看有人这样写,$CI = get_instance(); 要你自定义的类库中访问CodeIgniter的原始资源,你必须使用 get_instance() 函数...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn