我有这个index.php文件
# Loading configurations.
loadConfiguration('database');
loadConfiguration('site');
# Initializing the database connection.
$db = new database($c['db']['host'], $c['db']['username'], $c['db']['password'], $c['db']['name']);
unset($c['db']['password']);loadConfiguration()设置为:
function loadConfiguration($string) { require(path.'config/'.$string.'.con.php'); }我检查了database.con.php和site.con.php在config/文件夹中。我得到的唯一错误是一个注意事项:未定义变量: c,在下面一行中
$db = new database($c['db']['host'], $c['db']['username'], $c['db']['password'], $c['db']['name']);这是database.con.php
# Ignore.
if (!defined('APP_ON')) { die('Feel the rain...'); }
$c['db']['host'] = 'localhost';
$c['db']['username'] = 'root';
$c['db']['password'] = '';
$c['db']['name'] = 'name';这是site.con.php
# Ignore.
if (!defined('APP_ON')) { die('Feel the rain...'); }
/* Site informations */
$c['site']['name'] = 'Namek';
$c['site']['mail'] = 'mail@whocares.com';
$c['site']['enable-email'] = false;
$c['site']['debug-mode'] = true;我做错什么了?
发布于 2011-01-18 14:51:51
required代码在函数中执行,因此$c是局部变量,在全局范围内不可访问。
您可以使用一些花哨的设计模式来使$c成为全局的(例如,使用注册表(R::$c,R::get('c'),a Environment ($this->env->c),.)或者,您可以更改配置加载程序函数,以返回文件的名称和外部的require:
require_once configFile('database');
function configFile($name) {
return path . 'config/' . $name . '.con.php';
}发布于 2011-01-18 15:08:07
请不要根据安全目的使用全局变量。您可以在注册表变量中设置变量,然后访问它。
https://stackoverflow.com/questions/4725453
复制相似问题