What is the best way (coding) to save the system configuration in a PHP application?

Note. The configuration is stored in the PHP file, config.php .

I saw it differently, here is a short list of examples (I store DB information in these examples):

Constants: global, readonly

 define('DB_USER','user12'); define('DB_PASS','21user'); 

Using the GLOBALS array: global, mutable, repeating, mixed with other global values

 $GLOBALS['DB_USER']='user12'; $GLOBALS['DB_PASS']='21user'; 

Using a non-global array, but raised by globaly: perhaps worse than the second option

 $config=array(); ... $config['DB_USER']='user12'; $config['DB_PASS']='21user'; ... global $config; mysql_connect('localhost',$config['DB_USER'],$config['DB_PASS']); 

Defining class properties: (global, enumerated)

 class Config { public $DB_USER='user12'; public $DB_PASS='21user'; } 

Criteria / Parameters / Features:

  • ease of coding: you will not want to check if this parameter exists, or to initialize it
  • ease of modification: non-programmer / layman can easily change settings
  • stored in a clean place: does not mix with other variables (can be stored in the submatrix)
  • Change runtime: in some cases, other developers can easily modify existing settings.

You may need to change the configuration for a while while the system is running, so option 1 is no longer possible. The third option is also not too clean.


When writing this, I get a big warning that the discussion is subjective and closed. Therefore, please follow this topic and give reasonable reasons for your answers.


This is a fairly obvious question, and given that I am well acquainted with the different answers, you may ask, why am I doing all this fuss? The fact is that I am developing a framework and, unlike another structure (* ahem * joomla * ahem *), I do not want to go through their error, throwing in an erroneous solution, which ultimately needs to be changed / reassigned in the future .


Edit: First, the location of the configuration file does not concern me. I make sure that people can easily change their location if they want, but that will not be a requirement. Firstly, cheap web hosts do not allow this, and secondly, as far as security goes, this is really not a good option. What for? Because the structure must know where the config is located. Indeed, security through obscurity does not work . I would rather fix all of the RFI and XSS (for example) than being paranoid by hiding the configuration file under several layers.

+7
php config system
source share
4 answers

Why not use Zend_Config? It creates a common interface for configuration parameters that can be stored in a confing file or database (with the appropriate adapter). And it is light; you do not need to enter the entire Zend structure in order to use it.

By the way, since you are creating a framework, you should minimize pollution of the global namespace. Something like your 3rd option, and if you focus only on 5.3, look at using the correct namespaces.

+2
source share

Hard-coded data may not be an option when the people performing the reconfiguration are not code. Consider using parse_ini_file () .

+4
source share

A bit late, but it may interest you: http://milki.include-once.org/genericplugins/genconfig.html

It provides a simple API for editing PHP configuration files in place. It stores comments and other code in tactics. And it allows you to use the global $ config array / ArrayObject and define constants. It works almost automatically in conjunction with plugin configuration comments. However, this is a lot of code. But it might be worth checking out the concept. (I also use readable config.php as it seems to be the most useful configuration format for me.)

+1
source share

Place the shared file and include it where you need it. The advantage is when you go live or go to your test server, you just need to edit only this file and all the configs will be changed. Method 2 is better since it allows you to change it.

Remember, as soon as you connect to mysql, if you need to change the user and transfer, you need to reconnect

0
source share

All Articles