codeigniter 中 $this 和 $this->ci 之间的区别
您好,我正在使用 codeigniter ,在我的控制器构造函数中,有时我使用 $this
有时 $this->ci
使用这样的方法
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('form_validation');
$this->ci->load->library('catalog/CatalogManager');
}
function __construct()
{
parent::__construct ();
$this->ci = & get_instance ();
$this->load->library ( 'auth_lib' );
$this->load->library ( 'session' );
}
在两个构造函数中,我在传递数据到视图时 在上述两种情况下使用
$this->ci->data
和 $this->data
。
两者都没有给出错误,但我很困惑,正确的用法是什么。
请帮忙............
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('form_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...........
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有控制器都扩展了主 CI_Controller,因此调用类似
$this->load
的内容意味着访问父类 CI_Controller 内的父方法load()
。$this->ci
可以工作,因为使用$this->ci = &get_instance()
您再次调用对主控制器类的引用... 。如果您查看引导文件(IIRC。或 codeigniter.php 文件),就会发现函数get_instance()
,它除了返回(通过引用)CI_Controller 类的实例之外什么也不做。因此,基本上,调用
$this->ci->load
和$this->load
是完全相同的事情,只是第一个在 a 中是不必要的。控制器/模型/视图,因为系统已经在父类中这样做了(通过方法加载)。例如,如果您查看库,您会发现使用
$this->ci->method()
是必要的,因为您需要拥有可用的所有方法CI_Controller 是一种驱动整个框架的“超类”。查看加载器类和 CodeIgniter 类以了解 CI 内部的工作原理。
All controllers extend the main CI_Controller, so calling something like
$this->load
means accessing the parent methodload()
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 functionget_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.
同意上面的答案,但实际上,load是一个变量,而不是一个函数。它是CI_Loader类的一个对象,当你调用$this->load->libray()时,实际上调用的是CI_Loader中的library()函数。
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
没什么。它只是用来存储值。它就像一个变量。$this
is nothing. It just use to store the value. It just like a variable.