Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explain $CI =& get_instance();

Looking through codeigniter's source code,

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

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

like image 878
Hailwood Avatar asked Jan 19 '11 20:01

Hailwood


People also ask

What is CI CD in simple terms?

CI/CD is a method to frequently deliver apps to customers by introducing automation into the stages of app development. The main concepts attributed to CI/CD are continuous integration, continuous delivery, and continuous deployment.

What is meant by continuous integration?

Continuous integration refers to the build and unit testing stages of the software release process. Every revision that is committed triggers an automated build and test. With continuous delivery, code changes are automatically built, tested, and prepared for a release to production.

What is a CI environment?

Continuous Integration (CI) environment is a development practice of merging all developer code into a shared trunk/repository (the unnamed version of a file tree under a source control) several times a day. This practice allows developers to collaborate and detect problems early in the development phase.

Why we use CI CD?

Continuous Delivery (CD) allows you to take the code stored in the repository and continuously deliver it to production. CI/CD creates a fast and effective process of getting your product to market before your competition as well as releasing new features and bug fixes to keep your current customers happy.


2 Answers

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...

like image 159
ircmaxell Avatar answered Oct 24 '22 23:10

ircmaxell


get_instance() is a function defined in the core files of CodeIgniter. You use it to get the singleton reference to the CodeIgniter super object when you are in a scope outside of the super object.

I'm pretty sure it's defined in base.php or something similar.

like image 35
Wade Avatar answered Oct 24 '22 21:10

Wade