1 | <?php |
||
8 | class Buffer implements BufferInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var int |
||
12 | */ |
||
13 | protected $size; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $buffer; |
||
19 | |||
20 | /** |
||
21 | * @var GmpMathInterface |
||
22 | */ |
||
23 | protected $math; |
||
24 | |||
25 | /** |
||
26 | * @param string $byteString |
||
27 | * @param null|integer $byteSize |
||
28 | * @param GmpMathInterface $math |
||
29 | * @throws \Exception |
||
30 | */ |
||
31 | public function __construct($byteString = '', $byteSize = null, GmpMathInterface $math = null) |
||
46 | |||
47 | /** |
||
48 | * Return a formatted version for var_dump |
||
49 | */ |
||
50 | public function __debugInfo() |
||
57 | |||
58 | /** |
||
59 | * Create a new buffer from a hex string |
||
60 | * |
||
61 | * @param string $hexString |
||
62 | * @param integer $byteSize |
||
63 | * @param GmpMathInterface $math |
||
64 | * @return Buffer |
||
65 | * @throws \Exception |
||
66 | */ |
||
67 | public static function hex($hexString = '', $byteSize = null, GmpMathInterface $math = null) |
||
77 | |||
78 | /** |
||
79 | * @param int|string $integer |
||
80 | * @param null|int $byteSize |
||
81 | * @param GmpMathInterface|null $math |
||
82 | * @return Buffer |
||
83 | */ |
||
84 | public static function int($integer, $byteSize = null, GmpMathInterface $math = null) |
||
90 | |||
91 | /** |
||
92 | * @param integer $start |
||
93 | * @param integer|null $end |
||
94 | * @return Buffer |
||
95 | * @throws \Exception |
||
96 | */ |
||
97 | public function slice($start, $end = null) |
||
115 | |||
116 | /** |
||
117 | * Get the size of the buffer to be returned |
||
118 | * |
||
119 | * @return int |
||
120 | */ |
||
121 | public function getSize() |
||
125 | |||
126 | /** |
||
127 | * Get the size of the value stored in the buffer |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | public function getInternalSize() |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getBinary() |
||
152 | |||
153 | /** |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getHex() |
||
160 | |||
161 | /** |
||
162 | * @return \GMP |
||
163 | */ |
||
164 | public function getGmp() |
||
169 | |||
170 | /** |
||
171 | * @return int|string |
||
172 | */ |
||
173 | public function getInt() |
||
177 | |||
178 | /** |
||
179 | * @return Buffer |
||
180 | */ |
||
181 | public function flip() |
||
185 | |||
186 | /** |
||
187 | * @param BufferInterface $other |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function equals(BufferInterface $other) |
||
195 | } |
||
196 |
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.