global $var; Is Not Working!??!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

global $var; Is Not Working!??!

Post by nickvd »

Why does this work...

Code: Select all

class meh {
   function bah(){
      echo "whoo?";
   }
}

$moo = new meh();
$moo->bah();

Output, As Expected: "whoo?"
but... if I try to access $moo from inside a function:

Code: Select all

class meh {
   function bah(){
      echo "whoo?";
   }
}

$moo = new meh();

function meee(){
   global $moo;
   $moo->bah();
}

meee();

Output, not quite expected...:
"Fatal error: Call to a member function bah() on a non-object in /var/www/blahblahblah.php on line huh?"
I've just started using this box to code php (Ubuntu v5.04) and due to it's ease of use, I'm also using xampp (http://www.apachefriends.org/en/xampp-linux.html), I'm using php v5.0.4 (heh nice coincidence)...

I've never seen anything like this on any other box I've used (all winxp), I can just create the object inside the function, but that's a pita... especially for the db class...

...help! :?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Your second php code works form me.
Ouputs whoo?

BTW, Im using PHP5 - there has been a big jump from ver 4 to ver 5. Dont know if this is a version issue though.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I've been using php5 for a long while now (pretty much since it's been out of beta), and I have never seen anything like this. I'm really desperate to use this box for development, as it'll be the only one available to me while i'm at school... Should I consider this a bug in php? Or should i contact the guys at the xampp development...

I'm so confuzed!
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I truly dont know because this seems logical and works on my PC.
Im using PHP 5.0.3 with Apache 2.0.52.
I dont think this can be a PHP bug - but you can try searching at bugs.php.net
Post Reply