difference between $this and $this->ci in codeigniter
hi i am using codeigniter , in my controller constructor sometimes i use $this
sometimes $this->ci
in two constructors i have use like this
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('fo开发者_开发技巧rm_validation');
$this->ci->load->library('catalog/CatalogManager');
}
function __construct()
{
parent::__construct ();
$this->ci = & get_instance ();
$this->load->library ( 'auth_lib' );
$this->load->library ( 'session' );
}
when passing data to view i use
$this->ci->data
and $this->data
in above two cases .
neither gives errors , but i am confused , what is the correct use.
please help...........
All controllers extend the main CI_Controller, so calling something like $this->load
means accessing the parent method load()
inside the parent class CI_Controller.
$this->ci
works because with $this->ci = &get_instance()
you're calling a reference to the main controller class...again. If you look in the bootstrap file (IIRC. Or the codeigniter.php file) there's the function get_instance()
, which does nothing but return (by reference) the instance of the CI_Controller class.
So, basically, calling $this->ci->load
and $this->load
are the same exact thing, only that the first is unnecessary within a Controller/Model/View because the system is already doing that in the parent class (through the method load).
If you have a look at libraries, for ex., you'll see instead that using $this->ci->method()
is necessary, because you need to have available all the methods of the CI_Controller, which is a kind of "super class" that drives the whole framework.
Have a look at the loader class and the CodeIgniter class to grasp how CI internally works.
Agree with the answer above, but actually, load is a variable, not a function. it is a object of the class CI_Loader, when you call $this->load->libray(), in fact it calls the library() function in CI_Loader.
$this
is nothing. It just use to store the value. It just like a variable.
精彩评论