1 | <?php |
||
8 | class Dispatcher implements DispatcherContract |
||
9 | { |
||
10 | /** |
||
11 | * The IoC container instance. |
||
12 | * |
||
13 | * @var \Magister\Services\Container\Container |
||
14 | */ |
||
15 | protected $container; |
||
16 | |||
17 | /** |
||
18 | * The registered event listeners. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $listeners = []; |
||
23 | |||
24 | /** |
||
25 | * The sorted event listeners. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $sorted = []; |
||
30 | |||
31 | /** |
||
32 | * Create a new event dispatcher instance. |
||
33 | * |
||
34 | * @param \Magister\Services\Container\Container $container |
||
35 | */ |
||
36 | public function __construct(Container $container = null) |
||
40 | |||
41 | /** |
||
42 | * Register an event listener with the dispatcher. |
||
43 | * |
||
44 | * @param string $event |
||
45 | * @param mixed $listener |
||
46 | * @param int $priority |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | public function listen($event, $listener, $priority = 0) |
||
56 | |||
57 | /** |
||
58 | * Determine if a given event has listeners. |
||
59 | * |
||
60 | * @param string $eventName |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | public function hasListeners($eventName) |
||
68 | |||
69 | /** |
||
70 | * Fire an event and call the listeners. |
||
71 | * |
||
72 | * @param string $event |
||
73 | * @param mixed $payload |
||
74 | * @param bool $halt |
||
75 | * |
||
76 | * @return array|null |
||
77 | */ |
||
78 | public function fire($event, $payload = [], $halt = false) |
||
104 | |||
105 | /** |
||
106 | * Get all of the listeners for a given event name. |
||
107 | * |
||
108 | * @param string $eventName |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getListeners($eventName) |
||
120 | |||
121 | /** |
||
122 | * Sort the listeners for a given event by priority. |
||
123 | * |
||
124 | * @param string $eventName |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | protected function sortListeners($eventName) |
||
138 | |||
139 | /** |
||
140 | * Register an event listener with the dispatcher. |
||
141 | * |
||
142 | * @param mixed $listener |
||
143 | * |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function makeListener($listener) |
||
154 | |||
155 | /** |
||
156 | * Create a class based listener using the IoC container. |
||
157 | * |
||
158 | * @param mixed $listener |
||
159 | * |
||
160 | * @return \Closure |
||
161 | */ |
||
162 | public function createClassListener($listener) |
||
178 | |||
179 | /** |
||
180 | * Remove a set of listeners from the dispatcher. |
||
181 | * |
||
182 | * @param string $event |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | public function forget($event) |
||
192 | } |
||
193 |
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.