解释 $CI =&获取实例(); [英] explain $CI =& get_instance();

查看:19
本文介绍了解释 $CI =&获取实例();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看codeigniter的源代码,

Looking through codeigniter's source code,

在它的辅助函数中我一直看到代码<代码>$CI =&get_instance();谁能向我解释一下这段代码是如何工作的?

in its helper functions I keep seeing code $CI =& get_instance(); can anyone please explain to me how this code works?

我知道它正在返回对 $CI 超级对象的引用,但是 get_instance() 来自哪里?

I get that it is returning a reference to the $CI super object, but where does get_instance() come from?

推荐答案

它基本上是一个单例设计模式使用函数而不是静态方法.

It's basically a Singleton Design Pattern that uses a function instead of a static method.

要深入了解,请查看 源代码

所以基本上,它不强制执行单例,但它是公共函数的快捷方式...

So basically, it doesn't enforce the singleton, but it's a shortcut to a public function...

实际上,现在我明白了.为了 PHP4 兼容性,他们必须做一个 double-global-variable-hack 让它正确返回引用.否则,这些参考资料会被搞砸.而且由于 PHP4 不支持静态方法(好吧,无论如何都是正确的),因此使用该函数是更好的方法.所以由于遗留原因它仍然存在......

Actually, now I understand. For PHP4 compatibility they had to do a double-global-variable-hack to get it to return the references properly. Otherwise the references would get all screwed up. And since PHP4 didn't have support for static methods (well, properly anyway), using the function was the better way. So it still exists for legacy reasons...

因此,如果您的应用程序仅是 PHP5,那么应该使用 CI_Base::get_instance(); 没有任何问题,而是完全相同...

So if your app is PHP5 only, there should be nothing wrong with doing CI_Base::get_instance(); instead, it's identical...

这篇关于解释 $CI =&amp;获取实例();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆