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

(12-14-2015, 11:13 PM)ivantcholakov Wrote: http://stackoverflow.com/questions/38033...s-constant

Very interesting:
PHP Code:
// Works as of PHP 7
define('ANIMALS', array(
ANIMALS[1]; // outputs "cat" 

Martin7483 Wrote:You have a multi language website, I think you would then want to change the language setting in your config file to match your visitors chosen language.
This seems like a pretty interesting example, and language is certainly a consideration on my site, but one would probably not want to change what's in our config file -- certainly not on a user-by-user basis. In our case, language is determined by the requesting url and the language for a given request would be fixed for the entire duration of a page request.

Martin7483 Wrote:making this kind of assumption could get you in to trouble down the road
I was wondering if it might and have not yet been able to identify any real risk except for the possibility of constant name collisions with 3rd party libraries. I think the likelihood of this is small. Also wondering if using namespaces might help.

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 {
SOME_VAR "some-value";

// in a controller:
echo ABCD_conf::SOME_VAR

InsiteFX Wrote:I define all my constants at the bottom of ./application/config/constants.php
Seems to me that will make it more difficult for you to upgrade CodeIgniter when you need to because that file is one of the core CI files. If CI chooses to add new constants to this file, you'll be forced to do a merge to make sure your added constants and CI's added constants are all in the file.

Messages In This Thread
RE: defining constants in a config file? - by sneakyimp - 12-15-2015, 10:22 AM

Theme © iAndrew 2016 - Forum software by © MyBB