tags:

views:

157

answers:

5

If I use a configuration file to store for example the sitename, database host, etc.. what is the best practice to handle this?

  1. $sitename="stackoverflow.com"; and then setting global $sitename in functions
  2. DEFINE(_SITENAME,"stackoverflow.com")
  3. function vars(){return array(sitename=>"stackoverflow");}
  4. ?

I would love a technical explanation on why should I pick one instead of the other.

Thank you!

+2  A: 

It's generally considered 'better' practice to keep application-level configuration directives in a separate configuration text or XML file. You can create a class that's loaded as a singleton instance that parses the file and gives you what you want, e.g:

$config->get('DB_HOSTNAME');

Have a look at Zend_Config.

karim79
+1  A: 

You can easily import configuration data as an ini file using the functions parse_ini_file or parse_ini_string. Your configuration would be stored in the same format as php.ini.

Jeff Ober
+1  A: 

I tend to use define() for "environmental" configuration, since referencing a constant should be faster than calling a new function. It's also conceptually in line with what I'm doing:

/**
 * Set the application mode. Valid options are "development" and "production"
 */
define('APP_MODE', 'development');

// Database Host
define('DB_HOST', 'localhost');

Another option is to use ini files (make sure they are not in the web root) and the parse_ini_file() function, although you should only run this once and use a singleton to access the variables after that. (Really, you should lazy-load it, only parsing the file when the first request is made.)

James Socol
What are the benefits of using a configuration file (like .ini or .xml) over using a php file that defines constants? I come from a compiled language background where the latter is not an option. So I intuitively implemented a separate config file. After which I realized that in php, I could not think of any benefits of this method over just defining constants in a separate php file.
Matthijs Wessels
+1  A: 

I like to use define since it would prevent you from accidentally changing the constants later. It also makes it easier to tell in your code that you are using a constant.

Others have mentioned using a config file - this is a good idea, but the primary reason for that in most languages is that you can change the configuration file without having to rebuild/deploy the application. Since PHP is not a compiled language though, I don't see much real benefit to using ini or XML config files in PHP.

Eric Petroelje
More of a style issue so not voting +/-. My issue with global constants is figuring out the context. Whereas using some sort of function driven config you could do getConfig("majorCategory.minorCategory.valueName").
David
A: 

I like to use a global singleton object, in-line with what karim79 suggests, and have the implementation of the singleton class read the configuration from a set of .ini files using parse_ini_file. It is also good practice to have separate .ini files for each server environment, e.g.

production.ini testlab.ini demoserver.ini

You can then auto-select the correct config to load, based on the current hostname. All of the logic to handle this should be placed in the singleton implementation.

John Collins