卡在一个通知中:未定义的变量:c

发布于 2024-10-12 14:14:40 字数 1328 浏览 2 评论 0原文

我有这个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'] = '[email protected]';
$c['site']['enable-email'] = false;
$c['site']['debug-mode'] = true;

我做错了什么?

I have this index.php file

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

And loadConfiguration() is set as follow:

function loadConfiguration($string) { require(path.'config/'.$string.'.con.php'); }

I checked that database.con.php and site.con.php are into the config/ folder.
And the only error i get is a Notice: Undefined variable: c in the following line

$db = new database($c['db']['host'], $c['db']['username'], $c['db']['password'], $c['db']['name']);

Here's the 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';

Here's the site.con.php

# Ignore.
if (!defined('APP_ON')) { die('Feel the rain...'); }

/* Site informations */
$c['site']['name'] = 'Namek';
$c['site']['mail'] = '[email protected]';
$c['site']['enable-email'] = false;
$c['site']['debug-mode'] = true;

What am i doing wrong?

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

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

发布评论

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

评论(2

儭儭莪哋寶赑 2024-10-19 14:14:40

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

您可以使用一些奇特的设计模式来使 $c 全局化(例如使用注册表 (R::$c, R::get('c' )、环境 ($this->env->c), ...) 或者您可以更改配置加载器函数以返回文件的名称和 require 之外:

require_once configFile('database');

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

The required code is executed within the function, so $c is a local variable and not accessible in global scope.

You may either use some fancy design pattern to make $c global (e.g. use a Registry (R::$c, R::get('c'), an Environment ($this->env->c), ...) or you could change your configuration loader function to return the name of the file and require outside of it:

require_once configFile('database');

function configFile($name) {
    return path . 'config/' . $name . '.con.php';
}
滴情不沾 2024-10-19 14:14:40

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

Please don't use global variable according security purpose. you can set your variable in registry variable and then access it.

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