我目前正在这样做:
$_GET 请求
$process = @$_GET['data_process'];
$id = @$_GET['data_id'];
$link = @$_GET['data_page'];
Run Code Online (Sandbox Code Playgroud)
$_POST 请求
$process = @$_POST['data_process'];
$id = @$_POST['data_id'];
$link = @$_POST['data_page'];
Run Code Online (Sandbox Code Playgroud)
虽然它看起来很麻烦.我该如何改进这个过程?
这就是我做的
function POST($key) {
return isset($_POST[$key]) ? $_POST[$key] : null;
}
function GET($key) {
return isset($_GET[$key]) ? $_GET[$key] : null;
}
Run Code Online (Sandbox Code Playgroud)
简单用法
$process = GET('data_process');
$id = GET('data_id');
$link = GET('data_page');
$process = POST('data_process');
$id = POST('data_id');
$link = POST('data_page');
Run Code Online (Sandbox Code Playgroud)
编辑:通过这种方式,您可以轻松指定默认值.例:
function POST($key,$default=null) {
return isset($_POST[$key]) ? $_POST[$key] : $default;
}
$process = POST('p','defaultvalue');
Run Code Online (Sandbox Code Playgroud)
@ yes123想要更多,你有它..
$R = new REQUEST();
$process = $R['data_process'];
$id = $R['data_id'];
$link = $R['data_page'];
Run Code Online (Sandbox Code Playgroud)
使用的类
class REQUEST implements \ArrayAccess {
private $request = array();
public function __construct() {
$this->request = $_REQUEST;
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->request[] = $value;
} else {
$this->request[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->request[$offset]);
}
public function offsetUnset($offset) {
unset($this->request[$offset]);
}
public function offsetGet($offset) {
return isset($this->request[$offset]) ? $this->request[$offset] : null;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2747 次 |
| 最近记录: |