views:

319

answers:

2

Hi, I seem to have this problem after upgrading to PHP 5.3 from 5.2.
The site runs off index.php which includes() various other utility functions then includes the correct page based on GET variable value.

Now one thing I cannot understand is that in xdebug I dont see $GLOBALS. The other bit is -

index.php:

include_once('includes/global.inc.php');  
include_once("classes/db.inc.php");  
$db = new db();  

global.inc.php:

$glob['dbusername'] = 'myusername';  
$glob['dbpassword'] = 'mypassword';
//etc  

db.inc.php declares a class called db:

class db  
{  
 function db()  
 {  
  global $glob;  
  $this->db = @mysql_connect($glob['dbhost'], $glob['dbusername'], $glob['dbpassword']);  
 }  
} // end of db class  

The issue is that if i put a breakpoint in db(), i cannot see $glob and the debugger says its uninitialized.

+1  A: 

I have had similar issues when using globals in includes. I never really got around to what it really was down to - it sometimes seemed includes had their own scope. (It can't work, of course, if you are including the file from within a function, because the include will inherit the function's scope.)

I bet it works if you use one of these:

global $glob;
$glob['dbusername'] = 'myusername';  
$glob['dbpassword'] = 'mypassword';

or

$GLOBALS["glob"]['dbusername'] = 'myusername';  
$GLOBALS["glob"]['dbpassword'] = 'mypassword';

the fact that you can't see $GLOBALS in your debugger is maybe because it's not a real variable, but a construct.

Pekka
No, I'm not including the file from within a function.
Ali
@Ali does it work if you prepend `global $glob` ?
Pekka
no pre-pending global $glob in global.inc.php it does not work...
Ali
@Ali That is really odd. Can you do a number of test outputs at each step (in index.php after each line for example)?
Pekka
Beats me guys. I have restored my PHP 5.2 setup from recycle bin and tried debugging the same code and it works. $GLOBALS is still not shown in both versions however the code works in that declaring $glob as global causes it to be initialized correctly in db() in PHP 5.2
Ali
Can you try the `print_r()` step by step output to see where `$glob` goes lost? Are you 100% sure this is down to $glob and not a mySQL error? Can you remove the `@` from the `mysql_connect()` and output any possible error using `mysql_error()`?
Pekka
OK, here is why the $glob appears to be empty in xdebug. This is a bug with xdebug when used with PHP 5.3. http://bugs.xdebug.org/view.php?id=376So it boils down to PHP not being able to connect to MySQL... hunting in PHP.ini now...
Ali
@Ali there should be an error message in the `mysql_connect`.
Pekka
+2  A: 

OK, here is why the $glob appears to be empty in xdebug on Eclipse. This is a bug with xdebug 2.0.5 when used with PHP 5.3. See http://bugs.xdebug.org/view.php?id=376

So it boils down to PHP not being able to connect to MySQL... (I dont know why i have enabled php_mysql.dll and apache error logs are clean and phpinfo() shows MySQL allright)

I'm hunting in PHP.ini now...

Ali