1 | <?php |
||
15 | class ConversationSubscriber implements EventSubscriberInterface |
||
16 | { |
||
17 | /** |
||
18 | * Returns all the events that this subscriber handles, and which method |
||
19 | * handles each one |
||
20 | * |
||
21 | * @return array |
||
22 | */ |
||
23 | public static function getSubscribedEvents() |
||
24 | { |
||
25 | return array( |
||
26 | 'team.abandon' => array( |
||
27 | array('onTeamMembershipChange'), |
||
28 | array('onTeamLeave') |
||
29 | ), |
||
30 | 'team.kick' => array( |
||
31 | array('onTeamMembershipChange'), |
||
32 | array('onTeamLeave') |
||
33 | ) |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Called every time a member is added/removed from a team |
||
39 | * |
||
40 | * @param TeamAbandonEvent|TeamJoinEvent|TeamKickEvent $event The event |
||
41 | * @param string $type The type of the event |
||
42 | */ |
||
43 | public function onTeamMembershipChange(Event $event, $type) |
||
51 | |||
52 | /** |
||
53 | * When a player leaves a team, remove them from every conversation that |
||
54 | * includes that team |
||
55 | * |
||
56 | * @param TeamAbandonEvent|TeamKickEvent $event The event |
||
57 | */ |
||
58 | public function onTeamLeave(Event $event) |
||
70 | } |
||
71 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: