如何在PHP中使用外部变量(例如POST / GET),错误级别为E_NOTICE

I'm looking for the best way how to use external variables in PHP with error level including E_NOTICE.

I have three possible ways, I would be happy, if you can give some hints on each or suggest a different approach that YOU like.

    1.
class WebApp {

    public static function _GET($Index) {
        if (isset($_GET[$Index])) {
            return $_GET[$Index];
        } else {
            return NULL;
        }
    }
}

// E_NOTICE, does not throw a notice:
echo WebApp::_GET('ID');

// E_NOTICE, throws a notice:
echo $_GET['ID'];

2.

class RequestSanitizer {
    const V_INTEGER = 1;
    const V_STRING = 2;
    const V_REAL = 3;

    public static function Sanitize($arr) {
        foreach ($arr as $key => $val) {
            if (array_key_exists($key, $_GET)) {
                switch ($val) {
                    case RequestSanitizer::V_INTEGER:
                        $_GET[$key] = $_GET[$key] + 0;
                        break;
                    case RequestSanitizer::V_STRING:
                        $_GET[$key] = $_GET[$key] + '';
                        break;
                    case RequestSanitizer::V_REAL:
                        $_GET[$key] = $_GET[$key] + 0;
                        break;
                }
            } else {
                $_GET[$key] = null;
            }
        }
    }
}

RequestSanitizer::Sanitize(array(
    'GraphID' => RequestSanitizer::V_INTEGER,  
    'UserName' => RequestSanitizer::V_STRING,  
    'Password' => RequestSanitizer::V_STRING,  
    'Price' => RequestSanitizer::V_REAL 
));

echo $_GET['GraphID'];

3.

if (isset($_GET['ID']) && ($_GET['ID']+0>0)) {
   echo $_GET['ID']
}

i'd use a Request class that encapsulates all Php "superglobals" and provides methods like "param()", "numParam()", "arrayParam()" and so on.

$req = new Request();
$user_id = $req->numParam('id');
 // user_id is guaranteed to be a valid integer or 0

I would use

if (isset($_GET['ID']) && ($_GET['ID']+0>0)) {
   echo (int)$_GET['ID']
}

with a casting to integer (int). If the value must be an integer.