1 | <?php |
||
13 | class BufferStream implements StreamInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | private $hwm; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $buffer; |
||
24 | |||
25 | /** |
||
26 | * @param int $hwm High water mark, representing the preferred maximum |
||
27 | * buffer size. If the size of the buffer exceeds the high |
||
28 | * water mark, then calls to write will continue to succeed |
||
29 | * but will return false to inform writers to slow down |
||
30 | * until the buffer has been drained by reading from it. |
||
31 | */ |
||
32 | 11 | public function __construct($hwm = 16384) |
|
37 | |||
38 | 2 | public function __toString() |
|
42 | |||
43 | 2 | public function getContents() |
|
50 | |||
51 | 1 | public function close() |
|
55 | |||
56 | 1 | public function detach() |
|
60 | |||
61 | 2 | public function getSize() |
|
65 | |||
66 | 1 | public function isReadable() : bool |
|
70 | |||
71 | 1 | public function isWritable() : bool |
|
75 | |||
76 | 2 | public function isSeekable() : bool |
|
80 | |||
81 | public function rewind() |
||
85 | |||
86 | public function seek($offset, $whence = SEEK_SET) |
||
90 | |||
91 | 3 | public function eof() : bool |
|
95 | |||
96 | 1 | public function tell() |
|
100 | |||
101 | /** |
||
102 | * Reads data from the buffer. |
||
103 | */ |
||
104 | 8 | public function read($length) |
|
120 | |||
121 | /** |
||
122 | * Writes data to the buffer. |
||
123 | */ |
||
124 | 8 | public function write($string) |
|
135 | |||
136 | 1 | public function getMetadata($key = null) |
|
144 | } |
||
145 |
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.