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.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
You do not have to pass them as params. They can be used anywhere. Your example will work just fine.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
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.
ShawnCplus
Code Monkey
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
I'll give you a pretty advanced example for the sole reason for you to learn from.
<?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:
// 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)
ShawnCplus
Code Monkey
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
Wow thanks so much for that. I thought you meant an actual abstract class, i.e:
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.
ShawnCplus
Code Monkey
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268