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 | * Get data as array |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | public function all() |
||
41 | |||
42 | /** |
||
43 | * Get data as array |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | public function data() |
||
51 | |||
52 | /** |
||
53 | * Collection count |
||
54 | * |
||
55 | * @return int|void |
||
56 | */ |
||
57 | public function count() |
||
61 | |||
62 | /** |
||
63 | * Get current |
||
64 | * |
||
65 | * @return Model |
||
66 | */ |
||
67 | public function current() |
||
71 | |||
72 | /** |
||
73 | * Get valid |
||
74 | * |
||
75 | * @return Model |
||
76 | */ |
||
77 | public function valid() |
||
81 | |||
82 | /** |
||
83 | * Get key |
||
84 | * |
||
85 | * @return Model |
||
86 | */ |
||
87 | public function key() |
||
91 | |||
92 | /** |
||
93 | * Get next |
||
94 | * |
||
95 | * @return Model |
||
96 | */ |
||
97 | public function next() |
||
101 | |||
102 | /** |
||
103 | * Get iterator |
||
104 | * |
||
105 | * @return ArrayIterator|Traversable |
||
106 | */ |
||
107 | public function getIterator() |
||
111 | } |
||
112 |
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: