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.
foo()function. Could we see the fullMySqlDatabase()class, then the implementation of the class will become more clear.