1 | <?php |
||
32 | class RoutingUserSource implements RoutingSourceInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var User An user instance. |
||
36 | */ |
||
37 | protected $user = null; |
||
38 | |||
39 | /** |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @param User $user An user instance. |
||
43 | * |
||
44 | * @author Dominik del Bondio <[email protected]> |
||
45 | * @since 0.11.0 |
||
46 | */ |
||
47 | public function __construct(User $user) |
||
51 | |||
52 | /** |
||
53 | * Retrieves the value for a given entry from the source. |
||
54 | * |
||
55 | * @param array $parts An array with the name parts for the entry. |
||
56 | * |
||
57 | * @return mixed The value. |
||
58 | * |
||
59 | * @author Dominik del Bondio <[email protected]> |
||
60 | * @since 0.11.0 |
||
61 | */ |
||
62 | public function getSource(array $parts) |
||
74 | } |
||
75 |
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: