text CodeIgniter - 何时使用get_instance()

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text CodeIgniter - 何时使用get_instance()相关的知识,希望对你有一定的参考价值。

If you are new to CodeIgniter you may be confused as to when you need to use get_instance and when you don't, it certainly tripped me up when I first came back to CodeIgniter after leaving the PHP community for some time.

Luckily it is pretty simple to remember.

If you are writing code that lives within a controller, a model or a view that is under CodeIgniter's control then you do not need to use get_instance.

If however you are writing your own custom libraries or something that sits outside of the MVC files then you do need to use it.

So, if you are inside a model you could do something like this;

$this->load->library('my_cool_library')
But if you are in a class you have written yourself then the $this object will not know about the CodeIgniter stuff, so you would do something like this;

$ci=& get_instance();
$ci->load->library("my_cool_library")
We use =& because we don't want to make a new copy of the CodeIgniter object, we want to use the original one.

以上是关于text CodeIgniter - 何时使用get_instance()的主要内容,如果未能解决你的问题,请参考以下文章

CodeIgniter 从控制器返回数据

使用 CodeIgniter 事务?

Codeigniter 2 index和__construct之间的区别以及__construct中的内容

何时在 mysql 中使用 TEXT 而不是 VARCHAR [重复]

删除 index.php codeigniter 3.0.6

何时不在 Web 应用程序中使用 MVC?