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?
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.
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.
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.
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.
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With