Welcome Guest, Not a member yet? Register   Sign In
defining constants in a config file?
#9

(12-15-2015, 10:22 AM)sneakyimp Wrote:
Martin7483 Wrote:If you are worried about misspelling and bugs, maybe you could do a combination. Use constants for the names of your config items instead of the values.
I had considered this but ruled it out. It just seems ridiculous and doesn't solve the problem of name collisions because constants are global AFAIK. I had considered one other approach: attaching all my constants to some class:
PHP Code:
// this class would be loaded by an autoloader I have set up if I put it in the right place
// OR i could define this class in some config file
class ABCD_conf {
 
 const SOME_VAR "some-value";
}

// in a controller:
echo ABCD_conf::SOME_VAR

How does using a constant prevent name collisions? If you are worried about that you should really read the docs on the usage of config files. When loading a config you can pass in a second argument that helps avoid name collisions

Quote:If you need to load multiple config files normally they will be merged into one master config array. Name collisions can occur, however, if you have identically named array indexes in different config files. To avoid collisions you can set the second parameter to TRUE and each config file will be stored in an array index corresponding to the name of the config file.

https://codeigniter.com/user_guide/libra...onfig-file
Reply


Messages In This Thread
RE: defining constants in a config file? - by Martin7483 - 12-16-2015, 03:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB