I am ready to force my classes to define a constant named COMMAND. If php would allow you to redefine constants from interfaces, it would look like
class RequestInterface { const COMMAND = "no-command-specified"; } class LoginRequest implements RequestInterface { const COMMAND = "loginuser"; public $username; public $password; } $request = new LoginRequest(); $request->username = "user"; $request->password = "blah";
Obviously this does not work. I am looking for a clean way for my queries to define COMMAND cosntant.
I considered the following options:
- The interface defines the getCommand method, and my request classes should implement it and return the command name as a string. But this is too much code to query
- Replace the interface with an abstract class. This looks strange because it usually requires an abstract class to define at least one method.
The interface becomes an abstract class and defines the protected variable $ command. It also has a getter method that returns $ this-> command ;. Children override the $ command protected property. I don't like the way mixing public VARIABLES (which should be a variable) with the protected variable VARIABLE, which in fact should not be modifiable and therefore should not be considered a variable in the first place.
class LoginRequest extends BaseRequest { protected $command = "loginuser"; public $username; public $password; }
What will be the cleanest way to achieve this?
inheritance oop php interface
Vladimir Hraban
source share