| 1 | <?php |
||
| 9 | class ArrayAccessIterator implements SeekableIterator |
||
| 10 | { |
||
| 11 | protected $array; |
||
| 12 | protected $index; |
||
| 13 | |||
| 14 | public function __construct(ArrayAccess $array, $startIndex = 0) |
||
| 19 | |||
| 20 | public function rewind() |
||
| 24 | |||
| 25 | public function valid() |
||
| 29 | |||
| 30 | public function next() |
||
| 34 | |||
| 35 | public function key() |
||
| 39 | |||
| 40 | public function current() |
||
| 44 | |||
| 45 | public function seek($position) |
||
| 49 | |||
| 50 | } |
||
| 51 |
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: