在类中使用全局数组

发布于 2024-11-10 14:13:53 字数 1251 浏览 4 评论 0原文

我的目标是从另一个 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 技术交流群。

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

发布评论

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

评论(3

挥剑断情 2024-11-17 14:13:53

所有 4 行上的 ["DATABASE"} = ["DATABASE"]

["DATABASE"} = ["DATABASE"] on all 4 lines.

薯片软お妹 2024-11-17 14:13:53

你没有正确配对你的括号

$this->m_User = $CONFIG["DATABASE"}["USERNAME"]; 
----------------------------------^

应该是

$this->m_User = $CONFIG["DATABASE"]["USERNAME"]; 

You aren't pairing your brackets properly

$this->m_User = $CONFIG["DATABASE"}["USERNAME"]; 
----------------------------------^

Should be

$this->m_User = $CONFIG["DATABASE"]["USERNAME"]; 
还如梦归 2024-11-17 14:13:53

此后第 16 行和第 3 行出现拼写错误。

["DATABASE"} = ["DATABASE"] 应该是`["DATABASE"] = ["DATABASE"]

You have typo error on line 16 and 3 after that.

["DATABASE"} = ["DATABASE"] should be `["DATABASE"] = ["DATABASE"]

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