CodeIgniter Forums
Store static variables? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Store static variables? (/showthread.php?tid=23849)



Store static variables? - El Forum - 10-23-2009

[eluser]industrial[/eluser]
Hi again!

In a performance perspective, what is the best way to statically store some configuration variables in CI?

Is the config class the right way to go? If so, by loading the config directly after
Code:
parent::Controller();
in each controller where it will be used? Like this:

Code:
function mycontroller() {
        parent::Controller();    
        $this->config->load('myfunkyconfig');
    }



Have a great weekend!


Store static variables? - El Forum - 10-23-2009

[eluser]BrianDHall[/eluser]
For pure, pure performance, and with the likelyhood that you need the config variables available in most page accesses to your site, open up config.php and define constants. Constants can't change in value, so PHP can handle them differently than regular variables.

Phil Sturgeon gave me this particular usage, and it is what I now have on all my sites in config.php:

Code:
if(strpos($_SERVER['SERVER_NAME'], 'local') !== FALSE)
{
    define('ENV', 'local');
}
elseif(strpos($_SERVER['SERVER_NAME'], 'dev.') === 0)
{
    define('ENV', 'dev');
}
elseif(strpos($_SERVER['SERVER_NAME'], 'qa.') === 0) {
    define('ENV', 'qa');
}
else
{
    define('ENV', 'live');
}

Now I use ENV constant to determine if SSL is enabled (SSL doesn't work on my localhost, for instance), if calls to CURL are made or short-circuited for testing purposes (again my localhost isn't setup for curl and I don't wanna bother), what database settings are used, etc etc.


Store static variables? - El Forum - 10-23-2009

[eluser]jedd[/eluser]
My suggestion ... stick it in a config file (application/config/yourappname.php) and then autoload it (application/config/autoload.php - $autoload['config'] array).

If you ever find that performance is an issue for you, and you track it down to the loading of a few bytes of data into an array ... a) it'll be hugely impressive, and b) you can then push out the config loads to the controllers that need the data.


Store static variables? - El Forum - 10-23-2009

[eluser]Ben Edmunds[/eluser]
[you beat me to it guys]

That should be fine. Whether from a database or a config file, config variables won't really impact performance. Unless your running some sort of high performance application it shouldn't be an issue.

If performance is that big of a concern putting the variables in the existing config.php file would have better performance since it is already being read by CI.


Store static variables? - El Forum - 10-23-2009

[eluser]jedd[/eluser]
I was going to mention constants (since you used the word static in your problem description) but thought they might not be relevant. Seeing Brian mention them now ... well, I should point out that if you have some constants you want everywhere, you should define them in application/config/constants.php


Store static variables? - El Forum - 10-23-2009

[eluser]BrianDHall[/eluser]
Oh, I would like to re-interate what Ben and jedd note - worrying about the performance of a few configuration variables is a 'premature optimization' and is, ipso facto, bad.

...QED.


Store static variables? - El Forum - 10-23-2009

[eluser]BrianDHall[/eluser]
[quote author="jedd" date="1256336483"]I was going to mention constants (since you used the word static in your problem description) but thought they might not be relevant. Seeing Brian mention them now ... well, I should point out that if you have some constants you want everywhere, you should define them in application/config/constants.php[/quote]

Ah, I didn't even know that existed!


Store static variables? - El Forum - 10-23-2009

[eluser]jedd[/eluser]
[quote author="BrianDHall" date="1256336601"]
Ah, I didn't even know that existed![/quote]

Take comfort!

I only noticed it a couple of months ago, while I was busy adding constants into my config.php file .. proudly thinking how tidy I was being, and then wondering 'hey, what's this constants file do?', and then feeling a touch blonde.


Store static variables? - El Forum - 10-23-2009

[eluser]Ben Edmunds[/eluser]
Haha, I didn't know about that either! I'll definitely be using that.


Thanks,