如何创建全局配置文件?

发布于 2024-11-06 06:02:34 字数 601 浏览 4 评论 0原文

是否有可能创建一个包含类内可见的全局变量的配置文件?与此类似的内容:

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();

我不想通过构造函数传输到我的单例数据库连接器:

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

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'));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2024-11-13 06:02:34

每个人都有自己的喜好。我更喜欢将数据库设置存储在 webroot 之外的 .ini 中,然后为其指定 0600 chmod 值,以防止除所有者之外的任何人读取它。

.ini 示例如下所示:

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

然后您可以使用 php 函数 parse_ini_file 然后在您只需将其读入构造函数并将其解析为数组:

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 数组中获取了用户名等。

我极力避免将任何类型的数据库信息存储到 CONSTANT 或 GLOBAL 类型变量中。这样,$settings 只能用于该类函数,而不能用于其他函数,从而提供了额外的安全层。

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.

番薯 2024-11-13 06:02:34

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

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'); 代替 global $config别名。这样你的配置脚本就会将 $config 提取为构造函数中的局部变量,这就是你所需要的。

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.

×眷恋的温暖 2024-11-13 06:02:34

您可以尝试定义:

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

`用法:

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

You could try defines:

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

`Usage:

DB::getInstance(array(host_address, username, ...));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文