In my class, AController, I have the following method:
private function determineRequestMethod()
{
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$this->requestMethod = AController::POST;
}
else
{
$this->requestMethod = AController::GET;
}
}
This method is called from the constructor. POST and GET are class constants, and requestMethod is a class variable.
PS – $_SERVER['REQUEST_METHOD'] does work on my server.
I have updated the method to specifically check for GET and to throw an exception when the request method is not GET or POST:
private function determineRequestMethod()
{
if($_SERVER['REQUEST_METHOD'] == AController::POST)
{
$this->requestMethod = AController::POST;
}
else if($_SERVER['REQUEST_METHOD'] == AController::GET)
{
$this->requestMethod = AController::GET;
}
else
{
throw new Exception('Unexpected request method [' . $_SERVER['REQUEST_METHOD'] . '].');
}
}
Yes it is realiable, you could actually do:
If your class also supports other methods, you might want to put it in
elsecondition otherwise your code is fine.It should work elsewhere too 🙂