| | |
Using GET, POST, REQUEST in OOP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
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:
And then in my other documents I can just check the value of fooBar via:
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.
PHP Syntax (Toggle Plain Text)
class foo { function __ construct() { } function fooBar() { if($GET['param1']) return 1; return 0; } }; $obj = new foo();
And then in my other documents I can just check the value of fooBar via:
PHP Syntax (Toggle Plain Text)
<?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.
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.
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.
Google is your friend.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
•
•
•
•
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!
You do not have to pass them as params. They can be used anywhere. Your example will work just fine.
Google is your friend.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
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.
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.
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
•
•
•
•
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.
Thanks.
I'll give you a pretty advanced example for the sole reason for you to learn from.
USAGE:
Now, given that PHP 5.3 has been released I could've gotten REALLY fancy and allowed the
php Syntax (Toggle Plain Text)
<?php class Request { const TYPE_NONE = 0; const TYPE_ALPHA = 1; const TYPE_DIGIT = 2; const TYPE_ALNUM = 3; const GET = 10; const POST = 11; const COOKIE = 12; const SESSION = 13; public static function get($name, $method = null, $validation = null) { if ($method < self::GET && $validation === null) { $validation = $method; $method = self::GET; } else if ($method === null ) { $method = self::GET; } if ($validation === null) { $validation = self::TYPE_NONE; } $holder = null; switch ($method) { case self::GET: $holder = $_GET; break; case self::POST: $holder = $_POST; break; case self::COOKIE: $holder = $_COOKIE; break; case self::SESSION: $holder = $_SESSION; break; } if (!isset($holder[$name])) { return false; } $validator = null; switch ($validation) { case self::TYPE_ALNUM: $validator = 'alnum'; break; case self::TYPE_DIGIT: $validator = 'digit'; break; case self::TYPE_ALPHA: $validator = 'alpha'; break; } $ret_val = $holder[$name]; $valid_func = 'ctype_' . $validator; return (($validator === null) ? $ret_val : (( $valid_func($ret_val) ) ? $ret_val : null ) ); } }
USAGE:
php Syntax (Toggle Plain Text)
// basic GET param with no validation $blah = Request::get('blah'); // GET param with validation $blah = Request::get('blah', Request::TYPE_DIGIT); // POST param $blah = Request::get('blah', Request::POST); // POST param with validation $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.
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.
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.
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
Wow thanks so much for that. I thought you meant an actual abstract class, i.e:
One question though.. is there any significance behind the values of the constants or did you just arbitrarily pick them?
Thanks again.
PHP Syntax (Toggle Plain Text)
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.
•
•
•
•
Wow thanks so much for that. I thought you meant an actual abstract class, i.e:
PHP Syntax (Toggle Plain Text)
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.
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.
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.
![]() |
Similar Threads
- Create a post request programmatically in ASP.NET (C#)
- HTTP GET and POST REQUEST (Java)
- Post Bulk Data In AJAX request (JavaScript / DHTML / AJAX)
- A PHP REST POST request? (PHP)
- Sending a post request through url (Java)
Other Threads in the PHP Forum
- Previous Thread: Problem with mysql error...
- Next Thread: Php my admin for reporting
Views: 1000 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database dataentry date directory display download dynamic echo email error file files folder form forms freelancing function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap sorting source space speed sql structure syntax system table tutorial tutorials update updates upload url validation validator variable video web xml youtube zend






