SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Quick question

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Quick question

    Hey there, i have a quick question to just make sure my idea is correct.

    Say i had made a class to receive information (from posts/gets). I want to access the information in an OO way could i do something like this

    PHP Code:
    public function info($name){
            if(
    $this->method 'get'){
                return 
    $this->$name $_GET[$name];
            } 
    mind you this is just a function of the class. I *think* it should work. i am just wondering if
    PHP Code:
    $name 
    in the
    PHP Code:
    $this-> 
    accesor will throw it off.

    Thanks!

    -Colin

  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)
    PHP Code:
    class magic {

    public 
    $method "";

    function 
    __construct($m){
    $this->method=$m;
    }

    public function 
    info($name){ 
            if(
    $this->method === 'get'){ // = is asssignment, == is same value, === is same value and same type
                
    $this->$name $_GET[$name]; 
                return 
    $this->$name;
            }  

    }
    }
    // spoofing a get var
    $_GET['newyear'] = "Happy new year";

    // instantiate and set the method
    $m = new magic('get');

    // look for a value assuming it is set
    echo $m->info('newyear'); 
    I'm presuming you are just testing out some OOP syntax, because this kind of magic class which obfuscates where a REQUEST var has originally come from is rarely a good idea. Its tempting to do, I agree, I did the same one -- and lived to regret it.

    By the way, what you are trying to do (including variable variables) is well covered in PHP5s time-saving but magic getters and setters as illustrated in this simple tutorial.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok. So what is it would you recommend to do? Just use the $_GET/$_POST or make functions?

    Thanks

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    I'm not sure I entirely agree with Cups, It's not really a good idea to use $_GET/$_POST at arbitrary points throughout your code and abstracting that can be a good idea. That said, passing around a object which simply contains them in another format isn't much better.

    Using calls to $_GET and $_POST heavily reduces flexibility. Are you sure you always want that variable to come from $_GET? You severely limit reusability by making that assumption. As such, the top level of your application should have access to $_GET/$_POST and pass on the required variables to functions that need them. The functions which actually do any processing involving the variables should be totally agnostic about where they originally came from.

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Yes, I don't see any point in creating a class for accessing GET/POST (unless you want to implement some advanced filtering methods). Like TomB, I believe the values should be passed to other functions and objects.

    But at times I have done some minimal abstracting of GET/POST by writing the values to a $request variable (array) and then when I call controllers I pass $request to them and they don't read $_GET/$_POST directly but fetch values from $request. Sometimes this allows me to call a controller from another controller and feed it my desired request values without affecting the $_GET/$_POST superglobals. I've heard it's not a good idea to reuse controllers but anyway I do it vary rarely.

  6. #6
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    Sometimes this allows me to call a controller from another controller and feed it my desired request values without affecting the $_GET/$_POST superglobals. I've heard it's not a good idea to reuse controllers but anyway I do it vary rarely.
    Off Topic:

    Controllers shouldn't ever need to be chained in this way because controllers should just take user input and use it to set the model's state. If you're chaining like this what you should really be doing is setting the model's state from the active controller and rendering the required view. If you're finding the need to call controllers, it almost certainly means you have model state and/or logic in the controller.


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
  •