1 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); |
||
3 | class MY_Controller extends CI_Controller { |
||
4 | protected $header_data = array(); |
||
5 | protected $body_data = array(); |
||
6 | protected $footer_data = array(); |
||
7 | public $global_data = array(); |
||
8 | |||
9 | 50 | public function __construct(){ |
|
10 | 50 | parent::__construct(); |
|
|
|||
11 | |||
12 | //FIXME: This is pretty much a phpUnit hack. Without it phpUnit fails here. We need a proper way to fake user/admin testing. |
||
13 | 50 | $this->global_data['user'] = ($this->ion_auth->user() ? $this->ion_auth->user()->row() : ['username' => '']); |
|
14 | 50 | $this->global_data['username'] = $this->User->username; |
|
15 | |||
16 | //TODO: Move this to a lib or something. |
||
17 | 50 | $this->global_data['analytics_tracking_id'] = $this->config->item('tracking_id'); |
|
18 | |||
19 | 50 | $this->global_data['theme'] = $this->User_Options->get('theme'); |
|
20 | 50 | if(ENVIRONMENT !== 'development') { |
|
21 | 50 | $css_path = "css/main.{$this->User_Options->get('theme')}"; |
|
22 | 50 | $this->global_data['complied_css_path'] = asset_url()."{$css_path}.".filemtime(APPPATH . "../public/assets/{$css_path}.css").".css"; |
|
23 | |||
24 | 50 | $js_path = 'js/compiled.min'; |
|
25 | 50 | $this->global_data['complied_js_path'] = asset_url()."{$js_path}.".filemtime(APPPATH . "../public/assets/{$js_path}.js").".js"; |
|
26 | } |
||
27 | 50 | } |
|
28 | |||
29 | 21 | public function _render_page(/*(array) $paths*/) : void { |
|
30 | //We could just use global, but this is the only var we need in both header+footer |
||
31 | 21 | $this->footer_data['page'] = $this->header_data['page']; |
|
32 | |||
33 | 21 | $this->header_data['show_header'] = (array_key_exists('show_header', $this->header_data) ? $this->header_data['show_header'] : TRUE); |
|
34 | 21 | $this->footer_data['show_footer'] = (array_key_exists('show_footer', $this->footer_data) ? $this->footer_data['show_footer'] : TRUE); |
|
35 | |||
36 | 21 | $this->load->view('common/header', ($this->global_data + $this->header_data)); |
|
37 | 21 | foreach(func_get_args() as $path) { |
|
38 | 21 | view_exists($path) or show_404(); //TODO (FIXME): This seems bad performance wise in the long run. Is there any reason to have it in production? |
|
39 | |||
40 | 21 | $this->load->view($path, ($this->global_data + $this->body_data)); |
|
41 | } |
||
42 | //using the union operator + makes sure global_data always takes priority |
||
43 | //SEE: http://stackoverflow.com/a/2140094/1168377 |
||
44 | 21 | $this->load->view('common/footer', ($this->global_data + $this->footer_data)); |
|
45 | 21 | } |
|
46 | public function _render_json($json_input, bool $download = FALSE, string $filenamePrefix = 'tracker') : void { |
||
60 | } |
||
61 | |||
126 |
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: