1 | <?php |
||
15 | final class Eoi extends BaseToken |
||
16 | { |
||
17 | /** |
||
18 | * End of input token name |
||
19 | */ |
||
20 | public const T_NAME = 'T_EOI'; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | private $offset; |
||
26 | |||
27 | /** |
||
28 | * Eoi constructor. |
||
29 | * @param int $offset |
||
30 | */ |
||
31 | 6 | public function __construct(int $offset) |
|
35 | |||
36 | /** |
||
37 | * @return int |
||
38 | */ |
||
39 | public function getOffset(): int |
||
43 | |||
44 | /** |
||
45 | * @return string |
||
46 | */ |
||
47 | 6 | public function getName(): string |
|
51 | |||
52 | /** |
||
53 | * @param int|null $offset |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getValue(int $offset = null): string |
||
60 | |||
61 | /** |
||
62 | * @return iterable|string[] |
||
63 | */ |
||
64 | public function getGroups(): iterable |
||
68 | |||
69 | /** |
||
70 | * @return int |
||
71 | */ |
||
72 | public function getBytes(): int |
||
76 | |||
77 | /** |
||
78 | * @return int |
||
79 | */ |
||
80 | public function getLength(): int |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | public function __toString(): string |
||
92 | } |
||
93 |
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.