Using GET, POST, REQUEST in OOP

Reply

Join Date: Oct 2006
Posts: 165
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Using GET, POST, REQUEST in OOP

 
0
  #1
Jul 9th, 2009
Hi all. I have a current PHP script which checks a number of GET, POST, and REQUEST variables. These are currently in a top-down PHP file and these variables control the flow of the application. I want to convert file to a PHP class, create an object and then access the class functions to perform actions in my application. I'm wondering are there any problems with creating a PHP like this:
  1. class foo {
  2. function __ construct() {
  3. }
  4. function fooBar() {
  5. if($GET['param1'])
  6. return 1;
  7. return 0;
  8. }
  9. };
  10.  
  11. $obj = new foo();

And then in my other documents I can just check the value of fooBar via:

  1. <?php if(fooBar()) {....} ?>

Now this isn't exactly what I'm trying to do but it illustrates it. Is this good? i.e. proper practice? If not, how can I accomplish what I'm trying to do.

Thanks.
Last edited by Barefootsanders; Jul 9th, 2009 at 5:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,233
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 169
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Using GET, POST, REQUEST in OOP

 
0
  #2
Jul 9th, 2009
Those are called superglobals and they will work inside a class. Also the way you called the method in the if statement is incorrect. I hope you know that.

FYI: _POST and _GET are part of _REQUEST. Make sure you use the specific request type. Using _REQUEST isn't the best thing to do.
Visit: HaloTuts for Halo Tutorials
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 165
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Using GET, POST, REQUEST in OOP

 
0
  #3
Jul 9th, 2009
Originally Posted by kkeith29 View Post
Those are called superglobals and they will work inside a class. Also the way you called the method in the if statement is incorrect. I hope you know that.

FYI: _POST and _GET are part of _REQUEST. Make sure you use the specific request type. Using _REQUEST isn't the best thing to do.

Thanks for the response. How can I use them then? I'm going to assume pass them in as parameters? The only problem with that is that my program relies on $_GET and $_POST for program flow. What I'd have to do is pass them in before the HTML starts to set them and then just check them. Does that sound right?
And yes, I know the if syntax isnt correct.. Just threw up a quick example. Thanks though!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,233
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 169
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Using GET, POST, REQUEST in OOP

 
0
  #4
Jul 9th, 2009
You do not have to pass them as params. They can be used anywhere. Your example will work just fine.
Visit: HaloTuts for Halo Tutorials
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 165
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Using GET, POST, REQUEST in OOP

 
0
  #5
Jul 9th, 2009
Originally Posted by kkeith29 View Post
You do not have to pass them as params. They can be used anywhere. Your example will work just fine.
O wow, sorry. I read your previous post wrong.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,538
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 256
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Using GET, POST, REQUEST in OOP

 
0
  #6
Jul 9th, 2009
As was noted before, they are superglobals, you can use them anywhere. However, I suggest that you don't. If you're going to go the OOP route then I would suggest abstracting dealing with GET/POST/COOKIE/SESSION variables to a class that deals with them specifically called Request or something along those lines that can appropriately sanitize/parse/etc.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 165
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Using GET, POST, REQUEST in OOP

 
0
  #7
Jul 9th, 2009
Originally Posted by ShawnCplus View Post
As was noted before, they are superglobals, you can use them anywhere. However, I suggest that you don't. If you're going to go the OOP route then I would suggest abstracting dealing with GET/POST/COOKIE/SESSION variables to a class that deals with them specifically called Request or something along those lines that can appropriately sanitize/parse/etc.
Abstraction is one thing I'm not too advanced with.. Could you provide an example of what you mean by an abstraction layer using the GET/POST/etc... variables? Any help would be appreciated.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,538
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 256
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Using GET, POST, REQUEST in OOP

 
0
  #8
Jul 9th, 2009
I'll give you a pretty advanced example for the sole reason for you to learn from.
  1. <?php
  2. class Request
  3. {
  4. const TYPE_NONE = 0;
  5. const TYPE_ALPHA = 1;
  6. const TYPE_DIGIT = 2;
  7. const TYPE_ALNUM = 3;
  8.  
  9. const GET = 10;
  10. const POST = 11;
  11. const COOKIE = 12;
  12. const SESSION = 13;
  13.  
  14. public static function get($name, $method = null, $validation = null)
  15. {
  16. if ($method < self::GET && $validation === null) {
  17. $validation = $method;
  18. $method = self::GET;
  19. } else if ($method === null ) {
  20. $method = self::GET;
  21. }
  22.  
  23. if ($validation === null) {
  24. $validation = self::TYPE_NONE;
  25. }
  26.  
  27. $holder = null;
  28. switch ($method) {
  29. case self::GET:
  30. $holder = $_GET;
  31. break;
  32. case self::POST:
  33. $holder = $_POST;
  34. break;
  35. case self::COOKIE:
  36. $holder = $_COOKIE;
  37. break;
  38. case self::SESSION:
  39. $holder = $_SESSION;
  40. break;
  41. }
  42.  
  43. if (!isset($holder[$name])) {
  44. return false;
  45. }
  46.  
  47. $validator = null;
  48. switch ($validation) {
  49. case self::TYPE_ALNUM:
  50. $validator = 'alnum';
  51. break;
  52. case self::TYPE_DIGIT:
  53. $validator = 'digit';
  54. break;
  55. case self::TYPE_ALPHA:
  56. $validator = 'alpha';
  57. break;
  58. }
  59.  
  60. $ret_val = $holder[$name];
  61. $valid_func = 'ctype_' . $validator;
  62. return (($validator === null) ? $ret_val : (( $valid_func($ret_val) ) ? $ret_val : null ) );
  63. }
  64. }

USAGE:
  1. // basic GET param with no validation
  2. $blah = Request::get('blah');
  3. // GET param with validation
  4. $blah = Request::get('blah', Request::TYPE_DIGIT);
  5. // POST param
  6. $blah = Request::get('blah', Request::POST);
  7. // POST param with validation
  8. $blah = Request::get('blah', Request::POST, Request::TYPE_ALNUM);

Now, given that PHP 5.3 has been released I could've gotten REALLY fancy and allowed the $validation parameter to be a lambda callback (I'm running 5.3 so I could've but I HIGHLY doubt you are so I didn't)
Last edited by ShawnCplus; Jul 9th, 2009 at 11:37 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 165
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Using GET, POST, REQUEST in OOP

 
0
  #9
Jul 10th, 2009
Wow thanks so much for that. I thought you meant an actual abstract class, i.e:
  1. abstract class foo {...}

One question though.. is there any significance behind the values of the constants or did you just arbitrarily pick them?

Thanks again.
Last edited by Barefootsanders; Jul 10th, 2009 at 9:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,538
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 256
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Using GET, POST, REQUEST in OOP

 
0
  #10
Jul 10th, 2009
Originally Posted by Barefootsanders View Post
Wow thanks so much for that. I thought you meant an actual abstract class, i.e:
  1. abstract class foo {...}

One question though.. is there any significance behind the values of the constants or did you just arbitrarily pick them?

Thanks again.
Well, arbitrary in the sense that they aren't language constants but I picked the names because their purpose is easy to determine.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1188 | Replies: 10
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC