Welcome Guest, Not a member yet? Register   Sign In
Help about get_instance() function
#1

Greetings everyone,

I have a simple little question and I appreciate it if you help me understand if what I have in mind is useful or not.

Consider this CodeIgntier function:

PHP Code:
function &get_instance()
{
    return 
CI_Controller::get_instance();


Is is really good practice and useful if I change it (if allowed) to this:

PHP Code:
function &get_instance()
{
    static 
$CI;
    isset(
$CI) OR $CI CI_Controller::get_instance();
    return 
$CI;


This is just an idea I had i mind and I was wondering if it is really useful.
Thanks in advance.
Follow me on Github, you may find interesting things!
Reply
#2

The $instance variable is already static inside CI_Controller. I think your suggestion might actually add a method call to the functionality, however marginal the execution time difference would be.
Reply
#3

(05-12-2018, 05:08 PM)ciadmin Wrote: The $instance variable is already static inside CI_Controller. I think your suggestion might actually add a method call to the functionality, however marginal the execution time difference would be.

It's not a suggestion, I just had that in mind and wanted to make it clear because I was confused whether it is good practice or not. If I understand your answser, the execution time might be better, even slightly, that's a good thing right?
Thanks a lot for your answer.
Follow me on Github, you may find interesting things!
Reply
#4

Er, the execution time might be worse using your approach, although only slightly.
Reply
#5

(This post was last modified: 05-12-2018, 07:26 PM by bkader. Edit Reason: Fixed smiley. )

(05-12-2018, 06:44 PM)ciadmin Wrote: Er, the execution time might be worse using your approach, although only slightly.

Oh I see! I understood the opposite! Thanks a lot.

EDITED:
I tested it, you were right, I got like +0.01xx in execution time Smile
Follow me on Github, you may find interesting things!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB