How to create a global configuration file?

Is it possible to create a configuration file with global variables that are visible inside the class? Something like this:

config.php:

$config['host_address'] = 'localhost'; $config['username '] = 'root'; $config['password'] = 'root'; $config['name'] = 'data'; 

db.php:

 include('config.php'); class DB { private $_config = array($config['host_address'], $config['username'], $config['password'], $config['name']); ... 

Current Property:

 private $ _config = array(); 

I do not want to pass my Singleton database connector through the constructor:

 DB::getInstance(array('localhost', 'root', 'root', 'data')); 
+6
source share
3 answers

Your problem is that you are trying to use an expression in a class definition here:

 class DB { private $_config = array($config['host_address'], ... 

This is syntactically incorrect (you can only use constant values ​​for this), and I did not expect it to find the intended area there. Instead, you should initialize this property in the constructor:

 class DB { private $_config; function __construct() { global $config; $this->_config = array($config['host_address'], $config['username'], $config['password'], $config['name']); } 

Or even more lazy, just use include('config.php'); instead of the alias global $config . This way your config script will retrieve $ config as a local variable inside the constructor that you need.

+4
source

Everyone has their own preferences. I prefer to store my database settings in .ini outside the web root, and then give it the value 0600 chmod so that someone other than the owner does not read it.

An .ini example would look like this:

 [database] driver = mysql host = localhost ;port = 3306 schema = yourdbname username = dbusername password = some_pass 

Then you can use the php parse_ini_file function, then in your constructor you just read this and parse it into an array:

 public function __construct($file = 'dbsettings.ini') { // @todo: change this path to be consistent with outside your webroot $file = '../' . $file; if (!$settings = parse_ini_file($file, TRUE)) throw new exception('Unable to open ' . $file . '.'); $dns = $settings['database']['driver'] . ':host=' . $settings['database']['host'] . ((!empty($settings['database']['port'])) ? (';port=' . $settings['database']['port']) : '') . ';dbname=' . $settings['database']['schema']; // if not PDO, this part needs to be changed parent::__construct($dns, $settings['database']['username'], $settings['database']['password']); } 

And alt, you have a simple and secure way to set up a database connection. This class was taken from the PDO extender class, so if you are not using PDO, you need to change this line, but as you can see, you get the username, etc. In the $settings array.

I would really like to avoid storing information about any type of database in a variable of type CONSTANT or GLOBAL . Thus, $settings is only available for this class function and nothing else, providing an extra bit of security level.

+11
source

You may try:

 define('host_address', 'root'); define('username', 'root'); 

`Use:

 DB::getInstance(array(host_address, username, ...)); 
0
source

Source: https://habr.com/ru/post/887877/


All Articles