访问类构造函数内的全局数组

发布于 2024-11-10 14:35:44 字数 1353 浏览 7 评论 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"];
        $this->m_Pass = $CONFIG["DATABASE"]["PASSWORD"];
        $this->m_Host = $CONFIG["DATABASE"]["HOSTNAME"];
        $this->m_Data = $CONFIG["DATABASE"]["DATABASE"];
        $this->m_bConnected = false;
        $this->Connect();
    }

    // ...
};

没有给出错误(除了数据库连接失败通知)。

我无法访问数组元素。例如,$CONFIG["DATABASE"]["USERNAME"] 返回一个空值,即使它是使用 config.php 中的 "user" 字符串初始化的

我应该如何修改我的代码,以便可以在类构造函数内访问此全局数组?

(注: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. I understand that accessing a global array inside class is not a good idea, but I want to do it so because of some reasons.

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"];
        $this->m_Pass = $CONFIG["DATABASE"]["PASSWORD"];
        $this->m_Host = $CONFIG["DATABASE"]["HOSTNAME"];
        $this->m_Data = $CONFIG["DATABASE"]["DATABASE"];
        $this->m_bConnected = false;
        $this->Connect();
    }

    // ...
};

There is no error given (except for the failed database connection notification).

I can't access to the array elements. For instance $CONFIG["DATABASE"]["USERNAME"] returns an empty value even though it was initialized with "user" string in the config.php.

How should I modify my code so that it can this global array can be accessible inside the class constructor?

(Note: PHP version is 5.3.0)

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

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

发布评论

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

评论(3

归途 2024-11-17 14:35:44

你的代码看起来是正确的,所以我认为你应该调试它。尝试在创建数据库类实例之前输出 $CONFIG,$CONFIG 可能会在代码中的某处重新定义/更改。并且不要只检查数组中的一个值 - 使用 var_dump/print_r 输出整个数组。

Your code looks right, so I think you should just debug it. Try to output $CONFIG before creating instance of Database class, $CONFIG may be redefined/changed somewhere in your code. And don't just check one value in array - output whole array with var_dump/print_r.

彻夜缠绵 2024-11-17 14:35:44

而不是

$CONFIG = array();

使用

$GLOBALS['CONFIG'] = array();

Instead of

$CONFIG = array();

use

$GLOBALS['CONFIG'] = array();
执笏见 2024-11-17 14:35:44

我认为全局在 __construct() 中不起作用。我不确定这是否是一个错误,或者它是按原样设计的。

对于代码,

<?php
class Test {
    public $value;

    function __construct() {
        global $value;
        $value = "I am a test.";
    }
}

$test = new Test();
echo $test->value;

当上面的 php 运行时,你将看不到任何内容。

但是,如果您不使用全局,而是使用 $this->value,则一切正常。

<?php
class Test {
    public $value;

    function __construct() {
        $this->value = "I am a test.";
    }
}

$test = new Test();
echo $test->value;

如果你坚持要找个理由。我认为 __construct() 可能是为了初始化属性而设计的。诸如 $this->value = $value 之类的代码在 __construct() 中使用较多。所以也许 php 设计者认为在 __construct() 中使用 global 不是一个好的做法。然而。毕竟我在 php 手册中找不到一个字提到它。

I think some how global is not working in __construct(). I am not sure if it is a bug or it is designed as it is.

For code

<?php
class Test {
    public $value;

    function __construct() {
        global $value;
        $value = "I am a test.";
    }
}

$test = new Test();
echo $test->value;

You will see nothing when above php runs.

However, if you do not use global, but use $this->value, everything works fine.

<?php
class Test {
    public $value;

    function __construct() {
        $this->value = "I am a test.";
    }
}

$test = new Test();
echo $test->value;

If you insist to get a reason. I think maybe __construct() is design to initialize the properties. Some code like $this->value = $value is using a lot in __construct(). So maybe the php designer thinks it is not good practice to use global in __construct(). However. I can not find a word mentioned it in php manual after all.

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