Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何创建全局配置文件?_Php_Mysql_Static_Configuration Files - Fatal编程技术网

Php 如何创建全局配置文件?

Php 如何创建全局配置文件?,php,mysql,static,configuration-files,Php,Mysql,Static,Configuration Files,是否有可能创建一个配置文件,其中包含类内部可见的全局变量?类似于此: 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($conf

是否有可能创建一个配置文件,其中包含类内部可见的全局变量?类似于此:

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']);
    ...
当前属性:

private $ _config = array();
我不想通过构造函数传输到我的Singleton数据库连接器:

DB::getInstance(array('localhost', 'root', 'root', 'data'));
您可以尝试定义:

define('host_address', 'root');
define('username', 'root');
`用法:

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

您的问题是,您试图在此处的类定义中使用表达式:

class DB
{
    private $_config = array($config['host_address'], ...
这在语法上是不正确的(只能使用常量值),我不希望它在这里找到预期的范围。您应该改为在构造函数中初始化此属性:

class DB
{
    private $_config;

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

甚至更懒惰,只需使用
include('config.php')
代替
全局$config
别名。这样,您的配置脚本将在构造函数中提取$config作为局部变量,这就是您所需要的。

每个人都有自己的首选项。我更喜欢将我的DB设置存储在webroot之外的.ini中,然后给它一个0600 chmod值,以防止除所有者之外的任何人读取它

示例.ini如下所示:

[database]
driver = mysql
host = localhost
;port = 3306
schema = yourdbname
username = dbusername
password = some_pass
然后可以使用php函数,然后在构造函数中读取该函数并将其解析为数组:

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']);
}
viola,你有一个简单而安全的方法来设置你的数据库连接。该类取自PDO扩展程序类,因此如果您不使用PDO,则需要更改该行,但正如您看到的,您可以在
$settings
数组中获得用户名等


我会尽量避免将任何类型的数据库信息存储到
常量
全局
类型变量中。通过这种方式,
$settings
仅对该类函数可用,其他功能不可用,从而提供了额外的安全层

@Brad F Jacobs这不是最好的选择,但我们没有必要的权限将配置文件放在webroot之外。@Brad F Jacobs举个例子证明,否则就不会发生这种情况