PHP 全局变量

发布于 2024-12-07 21:51:56 字数 1057 浏览 0 评论 0原文

我很困惑为什么最简单的事情不起作用。

我想要的只是访问包含文件中声明的变量。

我有一个名为 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 技术交流群。

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

发布评论

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

评论(5

初见 2024-12-14 21:51:56

我对你的代码做了一些更改,这对我有用。

database.php

<?php
// filename database.php begin
class MySqlDatabase
{
    public $connection;
    function MySqlDatabase()
    {
        $this->connection= mysql_connect('HOST','USER','PASSWORD');
        mysql_select_db('databaseName',$this->connection);
    }
}
$database = new MySqlDatabase();
// filename database.php end
?>

Something.php

<?php
require_once 'database.php';
function foo( )
{
    global $database ;
    $sql = "select * from tableName";
    $rs=mysql_query($sql,$database->connection);
    while($row=mysql_fetch_array($rs))
    {
        print_r($row);
    }
    //and remainig code goes here...
}
foo();
?>

您可能以错误的方式操作 $connection 变量。

I made some change in your code, this work for me.

database.php

<?php
// filename database.php begin
class MySqlDatabase
{
    public $connection;
    function MySqlDatabase()
    {
        $this->connection= mysql_connect('HOST','USER','PASSWORD');
        mysql_select_db('databaseName',$this->connection);
    }
}
$database = new MySqlDatabase();
// filename database.php end
?>

something.php

<?php
require_once 'database.php';
function foo( )
{
    global $database ;
    $sql = "select * from tableName";
    $rs=mysql_query($sql,$database->connection);
    while($row=mysql_fetch_array($rs))
    {
        print_r($row);
    }
    //and remainig code goes here...
}
foo();
?>

You might be manipulating $connection variable in wrong manor.

单调的奢华 2024-12-14 21:51:56

只有 3 个可能的原因

  • $database 未在全局范围内定义
  • database.php 未包含为文件
  • 某些拼写错误对读者不可见

There are only 3 possible reasons

  • $database defined not in the global scope
  • database.php being included not as a file
  • some typo invisible to reader
喜爱皱眉﹌ 2024-12-14 21:51:56

您可以使用 单例模式 或在数据库类中设置变量 static 。

You could use a singleton pattern or set the variable static in database class.

2024-12-14 21:51:56

如果您已包含该文件,则使用以下命令:

//something.php
require_once 'database.php';

   /* Function FOO starts a new scope and doesn't see database object declared in database.php 
    * Therefore, you pass on the database object from calling function and use that instead.
    */

function foo($dbObj){     

    $sql = "some sql stagement";
    mysql_query($sql,$dbObj->connection);
    //and remainig code goes here...

}

foo($database);

If you have included the file then use the following:

//something.php
require_once 'database.php';

   /* Function FOO starts a new scope and doesn't see database object declared in database.php 
    * Therefore, you pass on the database object from calling function and use that instead.
    */

function foo($dbObj){     

    $sql = "some sql stagement";
    mysql_query($sql,$dbObj->connection);
    //and remainig code goes here...

}

foo($database);
风追烟花雨 2024-12-14 21:51:56

好吧,我找到了问题所在。问题出在 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.

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