Codeigniter:获取实例 [英] Codeigniter: Get Instance
问题描述
Codeigniter中的获取实例的目的是什么?
What is the purpose of "Get Instance" in Codeigniter? How would you explain this to a total beginner?
推荐答案
确定,所以CodeIgniter中的一切都通过超级用户magic $ this
变量。这只适用于类,因为 $ this
基本上定义了当前类。
Ok, so everything in CodeIgniter runs through the super-magic $this
variable. This only works for classes, as $this
basically defines the current class.
$ this is there,allow you to do $ this-> load-> model('whatever');
Your controller is a class, so $this is there, allowing you to do $this->load->model('whatever');
在模型中,你也在使用一个类。这里略有不同,因为 $ this
只包含有用的东西,因为你从模型中扩展。仍然, $ this
仍然有效。
In models, you are also using a class. It is slightly different here, as $this
only contains useful stuff as you are extending from Model. Still, $this
is still valid.
当您使用助手或库时, 实例或 $ this
等效。
When you are using a helper or a library, you need to find that "instance" or $this
equivalent.
$ci =& get_instance();
...使 $ ci
即使你不在一个类中,也不在继承它的类中。
…makes $ci
contain the exact same stuff/code/usefulness as $this
, even though you are not in a class, or not in a class that inherits it.
这是对2个品脱后的初学者的解释,所以它是错误或关于正确。 ; - )
That's an explanation for total beginners after 2 pints, so it's either wrong or about right. ;-)
这篇关于Codeigniter:获取实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!