1 | <?php |
||
16 | class Controller |
||
17 | { |
||
18 | |||
19 | use DynamicGlobal; |
||
20 | |||
21 | /** |
||
22 | * @var string $layout |
||
23 | */ |
||
24 | public $layout = 'main'; |
||
25 | |||
26 | /** |
||
27 | * @var string $response |
||
28 | */ |
||
29 | protected $response; |
||
30 | |||
31 | public function __construct() |
||
35 | |||
36 | public function before() {} |
||
37 | |||
38 | /** Global bootable method */ |
||
39 | public static function boot() {} |
||
40 | |||
41 | /** |
||
42 | * Build variables and display output html |
||
43 | */ |
||
44 | public function getOutput() |
||
88 | |||
89 | public function after() {} |
||
90 | |||
91 | /** |
||
92 | * Set single global variable |
||
93 | * @param string $var |
||
94 | * @param string $value |
||
95 | * @param bool $html |
||
96 | */ |
||
97 | public function setGlobalVar($var, $value, $html = false) |
||
101 | |||
102 | /** |
||
103 | * Set global variables as array key=>value |
||
104 | * @param $array |
||
105 | */ |
||
106 | public function setGlobalVarArray(array $array) |
||
110 | |||
111 | /** |
||
112 | * Special method to set response of action execution |
||
113 | * @param string $response |
||
114 | */ |
||
115 | public function setResponse($response) |
||
119 | |||
120 | /** |
||
121 | * Get response of action rendering |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getResponse() |
||
128 | |||
129 | } |
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: