1 | <?php |
||
9 | class WebsiteController |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var Callable |
||
14 | */ |
||
15 | public $render; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | public $defaults = array(); |
||
21 | |||
22 | /** |
||
23 | * @var LoggerInterface |
||
24 | */ |
||
25 | public $logger; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * @param Callable $render Rendering callable that receives template file and variables context |
||
30 | * @param array $defaults Default variables context |
||
31 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
32 | */ |
||
33 | 6 | public function __construct( Callable $render, array $defaults, LoggerInterface $logger = null) |
|
39 | |||
40 | |||
41 | |||
42 | /** |
||
43 | * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request |
||
44 | * @param \Psr\Http\Message\ResponseInterface $response PSR7 response |
||
45 | * |
||
46 | * @return \Psr\Http\Message\ResponseInterface |
||
47 | */ |
||
48 | 5 | public function __invoke(Request $request, ResponseInterface $response, $args) { |
|
94 | } |
||
95 |
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: