Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Global Vars
    primarykey
    data
    text
    <p>I am breaking my head why the simplest thing is not working. </p> <p>All I want is to access the variable declared in the included file.</p> <p>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 </p> <pre><code>// filename database.php begin class MySqlDatabase(){ // code goes here } $database = new MySqlDatabase(); // filename database.php end </code></pre> <p>Now I included database.php in something.php and trying to access the $database variable like this</p> <pre><code>//something.php require_once 'database.php'; function foo(){ global $database; $sql = "some sql statement"; mysql_query($sql,$database-&gt;connection); //and remainig code goes here... } </code></pre> <p>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.</p> <p>For debugging purpose I added is_object() check in the database.php file and it is returning true.</p> <p>Also I tried to access the global variable using other options like </p> <pre><code>$database =&amp; $GLOBALS['database']; </code></pre> <p>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</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload