1 | <?php |
||
13 | class AggregateRootEventException extends Exception implements AggregateException |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $eventName; |
||
19 | |||
20 | /** |
||
21 | * @var Event |
||
22 | */ |
||
23 | protected $event; |
||
24 | |||
25 | /** |
||
26 | * @var ChildEventException[] |
||
27 | */ |
||
28 | protected $children = []; |
||
29 | |||
30 | /** |
||
31 | * Creates a new aggregate exception |
||
32 | * |
||
33 | * @param string $eventName |
||
34 | * @param Event $event |
||
35 | * @param array $failedListeners Array of arrays in form [$listener, $error] |
||
36 | */ |
||
37 | public function __construct($eventName, Event $event, array $failedListeners) |
||
53 | |||
54 | /** |
||
55 | * Associated event name |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getEventName() |
||
63 | |||
64 | /** |
||
65 | * Associated event |
||
66 | * |
||
67 | * @return Event |
||
68 | */ |
||
69 | public function getEvent() |
||
73 | |||
74 | /** |
||
75 | * Children exceptions |
||
76 | * |
||
77 | * @return AggregateChildEventException |
||
78 | */ |
||
79 | public function getChildren() |
||
83 | } |
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.