$_SERVER 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
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

$_SERVER in a class

Post by orbstra »

How would I use $_SERVER in a class? I am getting an error in my IDE
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

please be more specific... maybe post some code
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

there is nothing wrong with using $_SERVER in a class, it is a super global. Are you sure you've typed everything correctly?

Post some code.
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

Code: Select all

var $request = $_SERVER['REQUEST_URI']; //Gets Full URL
		var $dirs = parse_url($request, PHP_URL_PATH); //Gets all URL Dirs
		var $url = explode("/", $dirs); //Puts URL Dirs into array $url
'missing ; after decloration....'
'parse error } at end of class statement...' a bunch of those, although I have combed through my code and I can't find a single decimal out of place
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post by thomas777neo »

You should be able to use it anywhere as it is a global variable.

Perhaps post some code that you can be assisted further
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you cannot reference functions or variables in var's.. you may only assign it a default string, null, or numeric value.

What your wanting to do belongs in separate methods or the constructor.
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

thomas777neo wrote:You should be able to use it anywhere as it is a global variable.

Perhaps post some code that you can be assisted further

Code: Select all

class guav_engine
	{
		var $url;
		var $method;
		//Get the URL
		var $request = $_SERVER['REQUEST_URI']; //Gets Full URL
		var $dirs = parse_url($request, PHP_URL_PATH); //Gets all URL Dirs
		var $url = explode("/", $dirs); //Puts URL Dirs into array $url
		
		function guav_sys_method_get() //Split $method from $url
			{
				global $method, $url;
				m_catch(admin, admin);
				m_catch(home, sys);
				m_catch(blog, sys);
				m_catch(null, sys);
				m_catch(page, page);
				return $method;
			}
		
		$max = count($url); //Split the $dir from $url
		for ($i=1;$i==$max;$i++)
			{
				$dir[$i-1] = $url[$i];
			}

PS: just learned classes about 5 minutes ago, so super hard-core technical answers will prob confuse me.
thnx all
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I've already answered your question.
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

Jcart wrote:I've already answered your question.
How do I put it in a different method or constructor?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

php4 http://www.php.net/manual/en/language.o ... ructor.php

php5 http://www.php.net/manual/en/language.oop5.decon.php

php5 example:

Code: Select all

class guav_engine
        {
                var $url;
                var $method;

                var $request;
                var $dirs;
                var $url;

                function __construct()
                       {
                                //Get the URL
                                $this->request = $_SERVER['REQUEST_URI']; //Gets Full URL
                                $this->dirs = parse_url($request, PHP_URL_PATH); //Gets all URL Dirs
                                $this->url = explode("/", $dirs); //Puts URL Dirs into array $url
                }
               
                function guav_sys_method_get() //Split $method from $url
                        {
                                global $method, $url;
                                m_catch(admin, admin);
                                m_catch(home, sys);
                                m_catch(blog, sys);
                                m_catch(null, sys);
                                m_catch(page, page);
                                return $method;
                        }
               
                $max = count($url); //Split the $dir from $url
                for ($i=1;$i==$max;$i++)
                        {
                                $dir[$i-1] = $url[$i];
                        }
//....
Last edited by Kieran Huggins on Thu Dec 28, 2006 2:50 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

i'll assume your using php4

Code: Select all

class foobar
{
   var $request;
   var $dirs;

   function foobar()
   {
      $this->request = $_SERVER['REQUEST_URI'];
      $this->dirs = parse_url($this->request, PHP_URL_PATH);

      //...
   }
}
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

oh man i love you guys
Post Reply