| 1 | <?php |
||
| 21 | class BuildFactory |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @param $buildId |
||
| 25 | * |
||
| 26 | * @return Build |
||
| 27 | * |
||
| 28 | * @throws \Exception |
||
| 29 | */ |
||
| 30 | public static function getBuildById($buildId) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Takes a generic build and returns a type-specific build model. |
||
| 43 | * |
||
| 44 | * @param Build $build The build from which to get a more specific build type. |
||
| 45 | * |
||
| 46 | * @return Build |
||
| 47 | */ |
||
| 48 | public static function getBuild(Build $build) |
||
| 85 | } |
||
| 86 |
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: