SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    wrapping superglobals in Request class?

    is there any advantage in wrapping superglobals llike $_GET in $_POST in a Request wrapper class?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Not sure of your motivations for thinking of doing this, but this ongoing conversation might be of interest to you.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There are possible advantages, yes - but you'd have to implement it correctly.

    For example:
    PHP Code:
    Class RequestData{
        protected 
    $Get$Post$Cookie$Session$Files;
        function 
    __Construct(){
            
    $this->Get $_GET;
            
    $this->Post $_POST;
            
    $this->Cookie $_COOKIE;
            
    $this->Session $_SESSION;
            
    $this->Files $_FILES;
        }
        
    //etc

    This isn't the right way to do it, as all instances contain the same thing. However, you can do the following:

    PHP Code:
    Class RequestData{
        protected 
    $Get$Post$Cookie$Session$Files;
        function 
    __Construct(array $Get = array(), array $Post = array(), array $Cookie = array(), array $Session = array(), array $Files = array()){
            
    $this->Get $Get;
            
    $this->Post $Post;
            
    $this->Cookie $Cookie;
            
    $this->Session $Session;
            
    $this->Files $Files;
        }
        protected function 
    getValue($Key$Array){
            return 
    Array_Key_Exists($Key$this->$Array) ? $this->$Array[$Key] : false;
        }
        public function 
    Get($Key){
            return 
    $this->getValue($Key'Get');
        }
        public function 
    Post($Key){
            return 
    $this->getValue($Key'Post');
        }
        
    //etc
    }
    $RequestData = new RequestData($_GET$_POST$_COOKIE$_SESSION$_FILES); 
    This way you can simulate a request by instantiating this RequestData class with custom data and pass it to an object to parse as a real request - allowing you to perform many different tasks (through pseudo-requests) on a single request.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •