PHP 全局变量
我很困惑为什么最简单的事情不起作用。
我想要的只是访问包含文件中声明的变量。
我有一个名为 connection.php 的文件,其中有一个名为 MySqlDatabase 的类。在文件的底部,我创建了该类的实例并将其分配给名为 $database 的变量
// filename database.php begin
class MySqlDatabase(){
// code goes here
}
$database = new MySqlDatabase();
// filename database.php end
现在我将database.php包含在something.php中,并尝试像这样访问$database变量
//something.php
require_once 'database.php';
function foo(){
global $database;
$sql = "some sql statement";
mysql_query($sql,$database->connection);
//and remainig code goes here...
}
当我运行something.php时,它是预计全局变量 $database 在函数 foo() 中应该可用,但似乎变量为 null,我使用 is_object() 函数进行测试以检查对象是否可用,但它返回 false。
出于调试目的,我在 database.php 文件中添加了 is_object() 检查,它返回 true。
我还尝试使用其他选项访问全局变量,例如
$database =& $GLOBALS['database'];
但我仍然没有运气。然后我也使用 print_r() 函数打印了完整的 $GLOBALS 数组,但它没有 $database 变量,我还使用 get_define_vars() 检查过,但它也没有该变量。但我能够在包含的文件本身中看到它们,
两天来我一直在绞尽脑汁让这个简单的事情发挥作用,但我妥协了并将数据库连接代码复制粘贴到所有文件中。
I am breaking my head why the simplest thing is not working.
All I want is to access the variable declared in the included file.
I have file called connection.php in which I have a class called MySqlDatabase. In the bottom of the file I created the instance of the class and assiged it to variable called $database
// filename database.php begin
class MySqlDatabase(){
// code goes here
}
$database = new MySqlDatabase();
// filename database.php end
Now I included database.php in something.php and trying to access the $database variable like this
//something.php
require_once 'database.php';
function foo(){
global $database;
$sql = "some sql statement";
mysql_query($sql,$database->connection);
//and remainig code goes here...
}
When I run the something.php it is expected that the global variable $database should be available in the function foo() but it seems like the variable is null, I tested with is_object() function to check the object is available but it returning false.
For debugging purpose I added is_object() check in the database.php file and it is returning true.
Also I tried to access the global variable using other options like
$database =& $GLOBALS['database'];
But I still have no luck. I then printed the complete $GLOBALS array using print_r() function also but it doesnt have the $database variable, I also checked using get_defined_vars() but it also doesnt have the variable. But I am able to see them in the included file itself
I am breaking my head since two days to make this simple thing work but I compromised and copy pasting database connection code in all the files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我对你的代码做了一些更改,这对我有用。
database.php
Something.php
您可能以错误的方式操作
$connection
变量。I made some change in your code, this work for me.
database.php
something.php
You might be manipulating
$connection
variable in wrong manor.只有 3 个可能的原因
There are only 3 possible reasons
您可以使用 单例模式 或在数据库类中设置变量 static 。
You could use a singleton pattern or set the variable static in database class.
如果您已包含该文件,则使用以下命令:
If you have included the file then use the following:
好吧,我找到了问题所在。问题出在 amfphp 框架上。这个框架包括我的所有文件(something.php)。很难解释它,但简单来说,我将 database.php 包含在与 amfphp 框架相关的主 php 文件之一中。
谢谢你的建议。
Well, I figured out the problem. The issue is with the amfphp framework. This framework includes all my files (something.php). Its hard to explain it but in simple words I included the database.php in the one of the main php file related to the amfphp framework.
Thanks for you suggetions.