Global vars in a class.

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
V0oD0o
Forum Newbie
Posts: 5
Joined: Wed May 14, 2003 9:41 am

Global vars in a class.

Post by V0oD0o »

Hi all,

Is it possible to use global, pre-defined vars like $_SESSION["blahvar"] or $_SERVER["REQUEST_URI"] without actually *passing* them into the class itself?

As an example:

# Method
function cameFrom()
{
$this->cameFrom = urlencode($SERVER["HTTP_REFERER"]);
return $this->cameFrom;
} # End method

Thanks in advance,

Mike Pearce
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$_SESSION, $_SERVER and some more are super(or auto-)global arrays that can be accessed from anywhere.
http://www.php.net/manual/en/reserved.variables.php
V0oD0o
Forum Newbie
Posts: 5
Joined: Wed May 14, 2003 9:41 am

Post by V0oD0o »

Thanks for the quick reply volka.

That's what I though, although that function doesn't return anything, neither does it return anything if I use REMOTE_ADDR.

If I do: $this->cameFrom = "testvarcontent"; it returns the string ok.

What could it be? I've been looking round the web and not found a thing.

Thanks,

Mike
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it's $_SERVER not $SERVER ;)
the only superglobal (if I'm not blind, which might be) without a leading _ is $GLOBALS.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

$_SERVER not $SERVER
V0oD0o
Forum Newbie
Posts: 5
Joined: Wed May 14, 2003 9:41 am

Post by V0oD0o »

Thanks,

That was a typo in the post, in the actual class I have:

Code: Select all

function cameFrom()
        {
            $this->cameFrom = urlencode($_SERVERї"REQUEST_URI"]);
            #$this->cameFrom = urlencode($this->requestURI);
            return $this->cameFrom;
        } # End function
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

which version of php do you use?
if you make it

Code: Select all

function cameFrom()
        {
            echo '<pre>'; print_r($_SERVER); echo '</pre>'; // debug only, do not forget to remove
            $this->cameFrom = urlencode($_SERVER["REQUEST_URI"]);
            #$this->cameFrom = urlencode($this->requestURI);
            return $this->cameFrom;
        } # End function
what does it print?
V0oD0o
Forum Newbie
Posts: 5
Joined: Wed May 14, 2003 9:41 am

Post by V0oD0o »

Sorry for the delay!

I added that to my class and nothing was printed, the <pre> tags were, but nothing else.
V0oD0o
Forum Newbie
Posts: 5
Joined: Wed May 14, 2003 9:41 am

Post by V0oD0o »

OK, it turns out that I've only got PHP 4.0.6.

Which is why $_SERVER wasn't working.

However, $HTTP_SERVER_VARS don't want to play ball when used in the class either.

Thanks for your help!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$HTTP_SERVER_VARS is not superglobal. If you want to use it within the scope of a function or method you have to import the global scope for that variable

Code: Select all

function cameFrom()
        {
            global $HTTP_SERVER_VARS;
            echo '<pre>'; print_r($HTTP_SERVER_VARS); echo '</pre>'; // debug only, do not forget to remove
            $this->cameFrom = urlencode($HTTP_SERVER_VARS["REQUEST_URI"]);
            #$this->cameFrom = urlencode($this->requestURI);
            return $this->cameFrom;
        } # End function
Post Reply