1 | <?php |
||
7 | final class NullRequest implements RequestInterface |
||
8 | { |
||
9 | /** |
||
10 | * @param array<string, mixed> $query |
||
11 | */ |
||
12 | public function __invoke(?array $query = null): ResourceObject |
||
16 | |||
17 | 7 | public function hash(): string |
|
21 | |||
22 | 1 | /** |
|
23 | * @return ResourceObject |
||
24 | */ |
||
25 | 1 | public function request() |
|
29 | |||
30 | 1 | /** |
|
31 | * {@inheritDoc} |
||
32 | 1 | */ |
|
33 | public function withQuery(array $query): RequestInterface |
||
39 | |||
40 | 1 | /** |
|
41 | * {@inheritDoc} |
||
42 | 1 | */ |
|
43 | public function addQuery(array $query): RequestInterface |
||
49 | |||
50 | 1 | public function toUri(): string |
|
54 | |||
55 | public function toUriWithMethod(): string |
||
59 | |||
60 | /** |
||
61 | * @return self |
||
62 | */ |
||
63 | public function linkSelf(string $linkKey): RequestInterface |
||
69 | |||
70 | /** |
||
71 | * @return self |
||
72 | */ |
||
73 | public function linkNew(string $linkKey): RequestInterface |
||
79 | |||
80 | /** |
||
81 | * @return self |
||
82 | */ |
||
83 | public function linkCrawl(string $linkKey): RequestInterface |
||
89 | } |
||
90 |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.