| 1 | <?php |
||
| 19 | final class InvalidHeaderException extends \Error |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $header; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var mixed |
||
| 28 | */ |
||
| 29 | private $value; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * InvalidHeaderException constructor. |
||
| 33 | * @param string $message |
||
| 34 | * @param string $header |
||
| 35 | * @param mixed $value |
||
| 36 | */ |
||
| 37 | public function __construct(string $message, string $header, $value) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public function getHeader(): string |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return mixed |
||
| 55 | */ |
||
| 56 | public function getValue() |
||
| 60 | } |
||
| 61 |
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: