1 | <?php |
||
18 | class Opcode implements OpcodeInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var array|string[]|null |
||
22 | */ |
||
23 | protected static $opcodes; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $operation; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $operands; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $id; |
||
39 | |||
40 | /** |
||
41 | * @var Readable |
||
42 | */ |
||
43 | private $file; |
||
44 | |||
45 | /** |
||
46 | * @var int|null |
||
47 | */ |
||
48 | private $offset; |
||
49 | |||
50 | /** |
||
51 | * Opcode constructor. |
||
52 | * @param int $operation |
||
53 | * @param mixed ...$operands |
||
54 | */ |
||
55 | public function __construct(int $operation, ...$operands) |
||
60 | |||
61 | /** |
||
62 | * @param int $operation |
||
63 | * @return Opcode |
||
64 | */ |
||
65 | public function rebind(int $operation): Opcode |
||
71 | |||
72 | /** |
||
73 | * @param int $id |
||
74 | * @param Readable $file |
||
75 | * @param int $offset |
||
76 | * @return Opcode |
||
77 | */ |
||
78 | public function mount(int $id, Readable $file, int $offset = 0): Opcode |
||
86 | |||
87 | /** |
||
88 | * @return int |
||
89 | */ |
||
90 | public function getId(): int |
||
96 | |||
97 | /** |
||
98 | * @return int |
||
99 | */ |
||
100 | public function getOffset(): int |
||
106 | |||
107 | /** |
||
108 | * @return int |
||
109 | */ |
||
110 | public function getOperation(): int |
||
114 | |||
115 | /** |
||
116 | * @param int $id |
||
117 | * @return mixed|null |
||
118 | */ |
||
119 | public function getOperand(int $id) |
||
123 | |||
124 | /** |
||
125 | * @return iterable |
||
126 | */ |
||
127 | public function getOperands(): iterable |
||
131 | |||
132 | /** |
||
133 | * @return Readable |
||
134 | */ |
||
135 | public function getFile(): Readable |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function __toString(): string |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getName(): string |
||
167 | |||
168 | /** |
||
169 | * @param int $operation |
||
170 | * @return string |
||
171 | */ |
||
172 | public static function getOperationName(int $operation): string |
||
189 | } |
||
190 |
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.