PHP如何处理_GET/_POST而不重复代码库

chr*_*ris 0 php post get

我正在研究各种API.它有点像RESTful API减去当前的put/delete.使用GET/POST或希望它会.

我的问题是目前我不希望有镜像代码,如果可能的话,一个基于POST的行为,一个基于GET,其中两个字面上都是相同的代码.

所以我想弄清楚的是有一个解决方案,我可以使用POST/GET数据,我可以使用相同的代码库,但不必为$ _GET制作一个副本,另一个用于$ _POST?

我知道我可以做点什么

if($_POST)
{
//then work with the post data
}
if($_GET)
{
//then work with the get data
}
Run Code Online (Sandbox Code Playgroud)

但是这个方法的问题很明显,也许我只是在盒子外思考得太远,所以我不知道......想法?

Sha*_*ngh 5

是的,您可以为此创建通用功能.

function doOperation($var)
{
   //then work with the var data
}

if(isset($_POST))
{
   doOperation($_POST);
}

if(isset($_GET))
{
   doOperation($_GET);
}
Run Code Online (Sandbox Code Playgroud)