explain $CI =& get_instance();

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

To look deeper, check out the source code

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

Edit: 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…

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

Leave a Comment