943,910 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2038
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 9th, 2009
0

Using GET, POST, REQUEST in OOP

Expand Post »
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:
PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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 6:59 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jul 9th, 2009
0

Re: Using GET, POST, REQUEST in OOP

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.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jul 9th, 2009
0

Re: Using GET, POST, REQUEST in OOP

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
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!
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jul 9th, 2009
0

Re: Using GET, POST, REQUEST in OOP

You do not have to pass them as params. They can be used anywhere. Your example will work just fine.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Jul 9th, 2009
0

Re: Using GET, POST, REQUEST in OOP

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
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!
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jul 9th, 2009
0

Re: Using GET, POST, REQUEST in OOP

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.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jul 9th, 2009
0

Re: Using GET, POST, REQUEST in OOP

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jul 10th, 2009
0

Re: Using GET, POST, REQUEST in OOP

I'll give you a pretty advanced example for the sole reason for you to learn from.
php Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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 10th, 2009 at 12:37 am.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jul 10th, 2009
0

Re: Using GET, POST, REQUEST in OOP

Wow thanks so much for that. I thought you meant an actual abstract class, i.e:
PHP Syntax (Toggle Plain Text)
  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 10:15 pm.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jul 10th, 2009
0

Re: Using GET, POST, REQUEST in OOP

Wow thanks so much for that. I thought you meant an actual abstract class, i.e:
PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Problem with mysql error...
Next Thread in PHP Forum Timeline: Php my admin for reporting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC