Home > Backend Development > PHP Tutorial > php $CI =& get_instance();

php $CI =& get_instance();

WBOY
Release: 2016-08-08 09:28:36
Original
1463 people have browsed it

Beginner to PHP, I saw someone write like this, $CI =& get_instance();

To access the original resources of CodeIgniter in your custom class library, you must use the get_instance() function. This function returns a CodeIgniter super object.
Generally speaking, in your controller function, you can call any available through $this CodeIgniter function:
$this->load->helper('url');
$this->load->library('session');
$this->config->item( 'base_url');
etc.
$this, only works directly in your own controllers, models and views. When you want to use the CodeIgniter original class in a custom class, you can do this:
First, define Assign a CodeIgniter object to a variable:
$CI =& get_instance();
Once you define an object as a variable, you can use that variable name instead of $this:
$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 by reference:
$CI =& get_instance();
This is very important. Assigning a variable by reference will use the original CodeIgniter object, instead of Not creating a copy
Also, please note: If you use PHP 4, then it is best not to call get_instance() in the constructor of the class. PHP4 has problems referencing the CI super object located in the constructor, because the object only Exists only after the class is fully instantiated.

The above introduces php $CI =& get_instance();, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template