php 如何创建全局配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5971365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create global configuration file?
提问by luzny
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:
数据库.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'));
采纳答案by mario
Your problem is that you are trying to use an expressionin the class definition here:
您的问题是您试图在此处的类定义中使用表达式:
class DB
{
private $_config = array($config['host_address'], ...
That is syntactically incorrect (you can only use constant valuesfor 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.
甚至懒惰,只是使用include('config.php');
替代的global $config
别名。这样你的配置脚本将在构造函数中提取 $config 作为局部变量,这就是你所需要的。
回答by Jim W.
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.
每个人都有自己的喜好。我更喜欢将我的数据库设置存储在 webroot 之外的 .ini 中,然后给它一个 0600 chmod 值,以防止除所有者之外的任何人阅读它。
An example .ini will look like:
一个示例 .ini 将如下所示:
[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:
然后您可以使用 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']);
}
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.
中提琴,你有一个简单而安全的方式来设置你的数据库连接。此类取自 PDO 扩展器类,因此如果您不使用 PDO,则需要更改该行,但正如您所看到的,您会在$settings
数组中获取用户名等。
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.
我会极力避免将任何类型的数据库信息存储到CONSTANT
orGLOBAL
类型变量中。这样,$settings
仅可用于该类函数而没有其他功能,提供了额外的安全层。
回答by H. Stefan
You could try defines:
你可以尝试定义:
define('host_address', 'root');
define('username', 'root');
`Usage:
`用法:
DB::getInstance(array(host_address, username, ...));