1 | <?php |
||
18 | abstract class AbstractRoom implements RoomInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $id; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $alias; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $groups; |
||
34 | |||
35 | /** |
||
36 | * @var \Domains\Middleware\Storage |
||
37 | */ |
||
38 | protected $middleware; |
||
39 | |||
40 | /** |
||
41 | * @var ClientInterface |
||
42 | */ |
||
43 | protected $client; |
||
44 | |||
45 | /** |
||
46 | * @var Application |
||
47 | */ |
||
48 | protected $app; |
||
49 | |||
50 | public function __construct() |
||
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | public function id() |
||
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | public function alias() |
||
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | public function groups() |
||
86 | |||
87 | /** |
||
88 | * @return ClientInterface |
||
89 | */ |
||
90 | public function client() |
||
94 | |||
95 | /** |
||
96 | * @return \Domains\Middleware\Storage |
||
97 | */ |
||
98 | public function middleware() |
||
102 | |||
103 | /** |
||
104 | * Create subscribers storage |
||
105 | */ |
||
106 | protected function createSubscribersStorage() |
||
119 | |||
120 | /** |
||
121 | * Create middleware storage |
||
122 | * |
||
123 | * @param array $groups |
||
124 | */ |
||
125 | protected function setMiddleware(array $groups) |
||
142 | |||
143 | /** |
||
144 | * @return Client |
||
145 | */ |
||
146 | public function listen() |
||
150 | |||
151 | /** |
||
152 | * @param string $message |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function sendMessage($message) |
||
159 | } |
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.