• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
defining constants in a config file?

#1
I'd like to define some constants in a config file. For example, here's a config file:
PHP Code:
// application/config/my-constants.php
define("ABCD_MY_CONSTANT""my constant value"); 
And then in my controller:
PHP Code:
$this->config->load('my-constants');
echo 
"the value is " ABCD_MY_CONSTANT

I fully realize that the constants will exist in in the global scope of my application, but the prefix I use ("ABCD" in this case) should pretty much guarantee no name conflicts. I realize this is a bit old school, but I like it because the constants defined this way will be interpreted by our developers' IDEs such that we don't have to worry as much about devs incorrectly typing scalar strings when trying to refer to values defined in some config file. The typing of scalar constants (e.g., $this->config->item("some-scalar-string") ) is one of the biggest sources of bugs in my experience. Defining the constants such that the IDE makes them available via autocomplete seems to largely remedy this problem.

I welcome any input on this matter.
Reply

#2
If you intend to use constants widely as an alternative of the configuration options your system will loose the ability to change their values on the fly.
Reply

#3
(12-14-2015, 06:07 PM)ivantcholakov Wrote: If you intend to use constants widely as an alternative of the configuration options your system will loose the ability to change their values on the fly.
When would one ever want to change the values in a config file? Yes, you are certainly correct that constants cannot be changed during the execution of a script, but given the nature of these configuration files and the values they contain, I don't see this ever happening in practice. I.e., one will probably never need to change any of these types of values from their initial settings during script execution:
* file paths for log files and tmp directories
* email credentials
* database password, etc.

In my estimation, the biggest risk I run is that I might have some kind of name collision with some 3rd party library I might use. Or another risk is that other developers might consider the definition of these constants as unorthodox or confusing. Aside from these two risks, I see little else to be concerned about. Am I missing something?
Reply

#4
http://stackoverflow.com/questions/38033...s-constant
Reply

#5
(12-14-2015, 06:44 PM)sneakyimp Wrote: When would one ever want to change the values in a config file? Yes, you are certainly correct that constants cannot be changed during the execution of a script, but given the nature of these configuration files and the values they contain, I don't see this ever happening in practice.

Example:
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.

Agreed, for most config settings the need to change them on the fly is small. But making this kind of assumption could get you in to trouble down the road.

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.

PHP Code:
$config['my_config_item'] = 'my_config_value';
define('ABCD_MY_CONSTANT''my_config_item');

$this->config->load('your_config');
$value $this->config->item(ABCD_MY_CONSTANT); 
Reply

#6
I define all my constants at the bottom of ./application/config/constants.php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
(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(
    
'dog',
    
'cat',
    
'bird'
));
echo 
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 {
  const 
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.
Reply

#8
It's not a core file it's in the application folder

Core files are the ones in the system folder
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.