在类中使用全局数组
我的目标是从另一个 PHP 文件中定义的全局数组中检索一些数据。我的代码在“database.php”文件内运行,我想使用的数组位于“config.php”文件内。
我的代码如下:
config.php
$CONFIG = array();
// ...
$CONFIG["DATABASE"] = array();
$CONFIG["DATABASE"]["USERNAME"] = "user";
$CONFIG["DATABASE"]["PASSWORD"] = "pass";
$CONFIG["DATABASE"]["HOSTNAME"] = "127.0.0.1";
$CONFIG["DATABASE"]["DATABASE"] = "my_db";
// ...
database.php
require('config.php');
class Database
{
protected $m_Link;
private $m_User;
private $m_Pass;
private $m_Host;
private $m_Data;
private $m_bConnected;
public function __construct()
{
global $CONFIG;
$this->m_User = $CONFIG["DATABASE"}["USERNAME"]; // Line #16
$this->m_Pass = $CONFIG["DATABASE"}["PASSWORD"];
$this->m_Host = $CONFIG["DATABASE"}["HOSTNAME"];
$this->m_Data = $CONFIG["DATABASE"}["DATABASE"];
$this->m_bConnected = false;
$this->Connect();
}
// ...
};
给出的错误是:
解析错误:语法错误,意外 '}',期待 ']' C:...\database.php 第 16 行
我无法弄清楚我在这里做错了什么。
请帮我修复这个错误。
(注:PHP版本为5.3.0)
My aim is to retrieve some data from a global array which is defined in another PHP file. My code is running inside "database.php" file and the array I want to use is inside "config.php" file.
My code is as below:
config.php
$CONFIG = array();
// ...
$CONFIG["DATABASE"] = array();
$CONFIG["DATABASE"]["USERNAME"] = "user";
$CONFIG["DATABASE"]["PASSWORD"] = "pass";
$CONFIG["DATABASE"]["HOSTNAME"] = "127.0.0.1";
$CONFIG["DATABASE"]["DATABASE"] = "my_db";
// ...
database.php
require('config.php');
class Database
{
protected $m_Link;
private $m_User;
private $m_Pass;
private $m_Host;
private $m_Data;
private $m_bConnected;
public function __construct()
{
global $CONFIG;
$this->m_User = $CONFIG["DATABASE"}["USERNAME"]; // Line #16
$this->m_Pass = $CONFIG["DATABASE"}["PASSWORD"];
$this->m_Host = $CONFIG["DATABASE"}["HOSTNAME"];
$this->m_Data = $CONFIG["DATABASE"}["DATABASE"];
$this->m_bConnected = false;
$this->Connect();
}
// ...
};
The error given is:
Parse error: syntax error, unexpected
'}', expecting ']' in
C:...\database.php on line 16
I couldn't figure out what I'm doing wrong here.
Please help me fix this error.
(Note: PHP version is 5.3.0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有 4 行上的
["DATABASE"}
=["DATABASE"]
。["DATABASE"}
=["DATABASE"]
on all 4 lines.你没有正确配对你的括号
应该是
You aren't pairing your brackets properly
Should be
此后第 16 行和第 3 行出现拼写错误。
["DATABASE"} = ["DATABASE"]
应该是`["DATABASE"] = ["DATABASE"]You have typo error on line 16 and 3 after that.
["DATABASE"} = ["DATABASE"]
should be `["DATABASE"] = ["DATABASE"]