| 1 | <?php |
||
| 23 | abstract class AbstractValidationStatus implements ValidationStatusInterface { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Get the code. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $code; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Message. |
||
| 34 | * |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | private $message; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor. |
||
| 41 | * |
||
| 42 | * @param int $code The code. |
||
| 43 | * @param string $message The message. |
||
| 44 | */ |
||
| 45 | protected function __construct($code, $message) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the code. |
||
| 52 | * |
||
| 53 | * @return int Returns the code. |
||
| 54 | */ |
||
| 55 | public function getCode() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get the message. |
||
| 61 | * |
||
| 62 | * @return string Returns the message. |
||
| 63 | */ |
||
| 64 | public function getMessage() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set the code. |
||
| 70 | * |
||
| 71 | * @param int $code The code. |
||
| 72 | * @return ValidationStatusInterface Returns this validation status. |
||
| 73 | */ |
||
| 74 | public function setCode($code) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Set the message. |
||
| 81 | * |
||
| 82 | * @param string $message The message. |
||
| 83 | * @return ValidationStatusInterface Returns this validation status. |
||
| 84 | */ |
||
| 85 | public function setMessage($message) { |
||
| 89 | |||
| 90 | } |
||
| 91 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.