| 1 | <?php |
||
| 7 | class Finder |
||
| 8 | { |
||
| 9 | private $id; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Finder constructor. |
||
| 13 | * @param $id |
||
| 14 | */ |
||
| 15 | 9 | public function __construct($id) |
|
| 19 | |||
| 20 | /** |
||
| 21 | * |
||
| 22 | * Find node in tree by id |
||
| 23 | * |
||
| 24 | * @param AbstractNode $node |
||
| 25 | * @return bool|AbstractNode |
||
| 26 | */ |
||
| 27 | 9 | public function find(AbstractNode $node) |
|
| 54 | } |
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: