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.