| 1 | <?php declare(strict_types=1); |
||
| 16 | class NullResponse implements ResponseInterface |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @return int |
||
| 21 | */ |
||
| 22 | 1 | public function getDuration(): int |
|
| 26 | |||
| 27 | /** |
||
| 28 | * @return int |
||
| 29 | */ |
||
| 30 | public function getStatusCode(): int |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | 2 | public function getBody() : ? string |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @return boolean |
||
| 45 | */ |
||
| 46 | public function isModified() : bool |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return \DateTime |
||
| 53 | */ |
||
| 54 | public function getLastModified() : ?\DateTime |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return iterable |
||
| 61 | */ |
||
| 62 | public function getHeaders() : iterable |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $name |
||
| 69 | * @return iterable |
||
| 70 | */ |
||
| 71 | public function getHeader(string $name) : iterable |
||
| 75 | } |
||
| 76 |
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.