简体   繁体   English

PHP全局变量

[英]PHP Global 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. 我有一个名为connection.php的文件,其中有一个名为MySqlDatabase的类。 In the bottom of the file I created the instance of the class and assiged it to variable called $database 在文件的底部,我创建了该类的实例,并将其辅助到名为$ 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中包含了database.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...

}

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. 当我运行something.php时,预期全局变量$ database应该在foo()函数中可用,但似乎该变量为null,我使用is_object()函数进行了测试,以检查对象是否可用,但返回假。

For debugging purpose I added is_object() check in the database.php file and it is returning true. 出于调试目的,我添加了is_object()来检查database.php文件,它返回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. 然后,我也使用print_r()函数打印了完整的$ GLOBALS数组,但是它没有$ database变量,我也使用了get_defined_vars()进行了检查,但它也没有变量。 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. 为了使这个简单的事情生效,我从两天前就不知所措,但是我妥协了并在所有文件中复制粘贴数据库连接代码。

I made some change in your code, this work for me. 我对您的代码进行了一些更改,这对我来说很有效。

database.php 为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 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. 您可能在错误的庄园中操纵$connection变量。

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);

There are only 3 possible reasons 只有3种可能的原因

  • $database defined not in the global scope $ database不在全局范围内定义
  • database.php being included not as a file 并非以文件形式包含database.php
  • some typo invisible to reader 读者看不见的一些错字

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

Well, I figured out the problem. 好吧,我发现了问题所在。 The issue is with the amfphp framework. 问题出在amfphp框架上。 This framework includes all my files (something.php). 这个框架包括了我所有的文件(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. 很难解释,但简单地说,我将database.php包含在与amfphp框架相关的主要php文件之一中。

Thanks for you suggetions. 谢谢您的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM