Do some of you here on Stack Overflow think that this could be a good implementation or not?
For example I have an interface called RequestInterface
which has 5 constants:
interface RequestInterface {
const GET = 1;
const POST = 2;
const PUT = 3;
const DELETE = 4;
const HEAD = 5;
public function getType();
// ... some other methods declarations
}
And then for each constant a class which implements that interface, e.g. the PostRequest
:
class PostRequest implements RequestInterface {
public function getType() {
return RequestInterface::POST
}
}
The PUT request:
class PutRequest implements RequestInterface {
public function getType() {
return RequestInterface::PUT
}
}
And so for the other classes.
I know that using constants inside an interface is considered an Anti-Pattern by some, but what would you say about the example I made, and what could be a possible better solution for such a case?
EDIT: @pid you advise me to use the get_class() or is_a() function, so e.g. if I have another class which uses that RequestInterface I could get rid of those constants and simply use even instanceof to determine the type of the RequestInterface?
// inside another class which uses a RequestInterface
public function someMethod(RequestInterface $request) {
if ($request instanceof PostRequest) {
// Request is a post...
}
}