陷入通知:未定义的变量:c

时间:2011-01-18 14:49:17

标签: php notice

我有这个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;

我做错了什么?

2 个答案:

答案 0 :(得分:0)

require d代码在函数内执行,因此$c是一个局部变量,在全局范围内无法访问。

您可以使用一些花哨的设计模式使$c全局(例如使用注册表(R::$cR::get('c'),环境($this->env->c),... )或者您可以更改配置加载程序函数以返回文件的名称,并在其外部返回require

require_once configFile('database');

function configFile($name) {
    return path . 'config/' . $name . '.con.php';
}

答案 1 :(得分:0)

请不要根据安全目的使用全局变量。您可以在注册表变量中设置变量,然后访问它。