访问类构造函数内的全局数组
我的目标是从另一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码看起来是正确的,所以我认为你应该调试它。尝试在创建数据库类实例之前输出 $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.
而不是
使用
Instead of
use
我认为全局在 __construct() 中不起作用。我不确定这是否是一个错误,或者它是按原样设计的。
对于代码,
当上面的 php 运行时,你将看不到任何内容。
但是,如果您不使用全局,而是使用 $this->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
You will see nothing when above php runs.
However, if you do not use global, but use $this->value, everything works fine.
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.