1 | <?php |
||
12 | class Controller |
||
13 | { |
||
14 | |||
15 | use DynamicGlobal; |
||
16 | |||
17 | /** |
||
18 | * @var string $layout |
||
19 | */ |
||
20 | public $layout = 'main'; |
||
21 | |||
22 | /** |
||
23 | * @var string $response |
||
24 | */ |
||
25 | public $response; |
||
26 | |||
27 | |||
28 | public function __construct() |
||
32 | |||
33 | public function before() {} |
||
34 | |||
35 | /** |
||
36 | * Build variables and display output html |
||
37 | */ |
||
38 | public function getOutput() |
||
82 | |||
83 | public function after() {} |
||
84 | |||
85 | /** |
||
86 | * Set single global variable |
||
87 | * @param string $var |
||
88 | * @param string $value |
||
89 | * @param bool $html |
||
90 | */ |
||
91 | public function setGlobalVar($var, $value, $html = false) |
||
95 | |||
96 | /** |
||
97 | * Set global variables as array key=>value |
||
98 | * @param $array |
||
99 | */ |
||
100 | public function setGlobalVarArray(array $array) |
||
104 | |||
105 | /** |
||
106 | * Special method to set response of action execution |
||
107 | * @param string $response |
||
108 | */ |
||
109 | public function setResponse($response) |
||
113 | |||
114 | } |
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: