PHP $CI =& get_instance ();

Source: Internet
Author: User
Tags codeigniter

Beginner PHP To see someone write this, $CI =& get_instance ();

To access CodeIgniter's original resources in your custom class library, you must use the Get_instance () function. This function returns a CodeIgniter super object.

In general, you can call any of the available CodeIgniter functions in your controller function by $this:

$this->load->helper (' url ');
$this->load->library (' Session ');
$this->config->item (' Base_url ');
etc.
$this, it only works directly in your own controllers, models, and views. When you want to use the CodeIgniter primitive class in your custom class, you can do this:

First, define the CodeIgniter object to assign to a variable:

$CI =& get_instance ();

Once you define an object as a variable, you can replace the $this with that variable name:

$CI =& get_instance ();

$CI->load->helper (' url ');
$CI->load->library (' Session ');
$CI->config->item (' Base_url ');
etc.
Note: You will notice that the get_instance () function is passed in a referenced manner:

$CI =& get_instance ();

This is very important. Assigning a variable by reference causes the original CodeIgniter object to be used, instead of creating a copy

Also, please note: If you use PHP 4, it is best not to call Get_instance () in the constructor of the class. PHP4 has a problem referencing CI Super object in the constructor because the object exists only after the class is fully instantiated.

PHP $CI =& get_instance ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.