How to create global configuration file?

流过昼夜 提交于 2019-12-19 06:04:05

问题


Is there any possibility to create a configuration file with global variables that are visible inside the class? Something similar to 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 don't want to transmit through the constructor to my Singleton database connector:

DB::getInstance(array('localhost', 'root', 'root', 'data'));

回答1:


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

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

That is syntactically incorrect (you can only use constant values for that), and I wouldn't expect it to locate the intended scope there. What you should do instead is initialize this property in the construtor instead:

class DB
{
    private $_config;

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

Or even lazier, just use include('config.php'); in place of the global $config alias. That way your config script will extract $config as local variable within the constructor, which is all you need.




回答2:


Everyone has their own preferences. I prefer to store my DB settings in a .ini outside of the webroot and then give it a 0600 chmod value, to prevent anyone but the owner reading it.

An example .ini will look like:

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

Then you can use the php function parse_ini_file then in your constructor you just read that in 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 viola, you have a simple and secure way to setup your database connection. This class was taken from a PDO extender class, so if you are not using PDO you need to change that line, but as yo ucan see you get the username etc in a $settings array.

I would HIGHLY avoid storing any type of database information into a CONSTANT or GLOBAL type variable. This way, the $settings is only available to that class function and nothing else, providing an extra bit of security layer.




回答3:


You could try defines:

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

`Usage:

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


来源:https://stackoverflow.com/questions/5971365/how-to-create-global-configuration-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!