| 1 | <?php |
||
| 11 | class NotFoundException extends \Exception |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * NotFoundException constructor. |
||
| 15 | * @param string $message |
||
| 16 | * @param int $code |
||
| 17 | * @param Throwable|null $previous |
||
| 18 | */ |
||
| 19 | public function __construct($message = "", $code = 0, Throwable $previous = null) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @return string|void |
||
| 26 | */ |
||
| 27 | public function __toString() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * |
||
| 34 | */ |
||
| 35 | public function __wakeup() |
||
| 39 | |||
| 40 | } |
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: