Current location: Hot Scripts Forums » Programming Languages » PHP » PHP global variables don't work inside a class


PHP global variables don't work inside a class

Reply
  #1 (permalink)  
Old 12-20-04, 08:50 AM
wizkid's Avatar
wizkid wizkid is offline
Newbie Coder
 
Join Date: Nov 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Question PHP global variables don't work inside a class

guys need a bit of help here..

I'm trying to create a class where in that class i'm using some of the php's global variables. I don't understand why the variables doesn't contain any value if used inside a class. Here is a sample:

testclass.php contains:
PHP Code:

<?

class test {
   function 
test() {
      echo 
"test:" $HTTP_SERVER_VARS["SERVER_NAME"];
   }
}
?>
Then test.php contains:
PHP Code:

<?

  
include("testclass.php");
  
$myTest = new test;
?>
This should display "test:MyServerName" but i only get "test:". But when I try to place the script
PHP Code:

echo "test:" $HTTP_SERVER_VARS["SERVER_NAME"]; 

outside the class or place it in the test.php, it works fine.

Can anybody tell me why is it so? Or did I miss something? Is there something wrong with the code?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 12-20-04, 10:29 AM
Vulture Vulture is offline
Newbie Coder
 
Join Date: Dec 2004
Location: Scotland, UK
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
This works for me:
PHP Code:

class test {

   function 
test() {
    global 
$HTTP_SERVER_VARS;
      echo 
"test:" $HTTP_SERVER_VARS["SERVER_NAME"];
   }

Reply With Quote
  #3 (permalink)  
Old 12-20-04, 03:03 PM
wizkid's Avatar
wizkid wizkid is offline
Newbie Coder
 
Join Date: Nov 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Cool! Works fine. Thanks Vulture.
Can you/someone tell me why we need to declare global HTTP_SERVER_VARS in a class? coz it works even if we dont declare it as long as its not inside a class..
Reply With Quote
  #4 (permalink)  
Old 12-20-04, 03:19 PM
Vulture Vulture is offline
Newbie Coder
 
Join Date: Dec 2004
Location: Scotland, UK
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Because your creating a function (the class isn't the cause here) which you may want to use different variables, you have specify exactly what variables the function is permitted to use/alter. You could've sent the variable like this
PHP Code:

function test($name) {

          echo 
"test:".$name;
}
test($HTTP_SERVER_VARS['SERVER_NAME']); 
However, that would not be practical in this example since your sending a in-built variable which you could include in a "global" statement reducing the need to provide it as an argument. Hope thats explained it better, if not, i'll try and clear it up some more.
Reply With Quote
  #5 (permalink)  
Old 12-20-04, 03:31 PM
wizkid's Avatar
wizkid wizkid is offline
Newbie Coder
 
Join Date: Nov 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Cool! Thanks a lot. You explained it well.
Merry Christmas..
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The Art Nexus seeking PHP programmers TheArtNexus Job Offers & Assistance 5 02-26-08 03:08 AM
Redirection back to a page from form submit DAL Perl 11 03-21-05 02:45 PM
PHP Programer(s) needed to work on open source project with me blasto333 Job Offers & Assistance 0 11-19-04 06:55 PM
PHP Error Fairnie PHP 8 06-26-04 07:15 AM
How to convert php generated pages to SE friendly HTML or PHP(SE friendly) -CLASS 101 crippled PHP 1 11-16-03 06:37 AM


All times are GMT -5. The time now is 05:22 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.