1 | <?php |
||
14 | class Collection implements Countable, IteratorAggregate |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $data = array(); |
||
21 | |||
22 | /** |
||
23 | * Add to collection |
||
24 | * |
||
25 | * @param Model $model |
||
26 | */ |
||
27 | public function add(Model $model) |
||
31 | |||
32 | /** |
||
33 | * Collection count |
||
34 | * |
||
35 | * @return int|void |
||
36 | */ |
||
37 | public function count() |
||
41 | |||
42 | /** |
||
43 | * Get current |
||
44 | * |
||
45 | * @return Model |
||
46 | */ |
||
47 | public function current() |
||
51 | |||
52 | /** |
||
53 | * Get iterator |
||
54 | * |
||
55 | * @return ArrayIterator|Traversable |
||
56 | */ |
||
57 | public function getIterator() |
||
61 | } |
||
62 |
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 implementation 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 interface: