卡在一个通知中:未定义的变量:c
我有这个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
require
d 代码在函数内执行,因此$c
是局部变量,无法在全局范围内访问。您可以使用一些奇特的设计模式来使
$c
全局化(例如使用注册表 (R::$c
,R::get('c' )
、环境 ($this->env->c
), ...) 或者您可以更改配置加载器函数以返回文件的名称和require
之外:The
require
d 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 andrequire
outside of it:根据安全目的,请不要使用全局变量。您可以在注册表变量中设置变量,然后访问它。
Please don't use global variable according security purpose. you can set your variable in registry variable and then access it.