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 | 28 | public function __construct(){ |
|
10 | 28 | 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 | 28 | $this->global_data['user'] = ($this->ion_auth->user() ? $this->ion_auth->user()->row() : ['username' => '']); |
|
14 | 28 | $this->global_data['username'] = $this->User->username; |
|
15 | |||
16 | 28 | $this->global_data['analytics_tracking_id'] = $this->config->item('tracking_id'); |
|
17 | |||
18 | 28 | $this->global_data['theme'] = $this->User_Options->get('theme'); |
|
19 | 28 | if(ENVIRONMENT === 'production') { |
|
20 | $this->global_data['compiled_css_path'] = function () { |
||
21 | $css_path = "css/main.{$this->global_data['theme']}"; |
||
22 | return asset_url() . $css_path . '.' . filemtime(APPPATH . "../public/assets/{$css_path}.css") . '.css'; |
||
23 | }; |
||
24 | |||
25 | $js_path = 'js/compiled.min'; |
||
26 | $this->global_data['compiled_js_path'] = asset_url() . $js_path . '.' . filemtime(APPPATH . "../public/assets/{$js_path}.js") . '.js'; |
||
27 | } |
||
28 | 28 | } |
|
29 | |||
30 | 17 | public function _render_page(/*(array) $paths*/) : void { |
|
31 | 17 | $this->header_data['title'] = ($this->header_data['title'] !== 'Index' ? $this->header_data['title'].' - Manga Tracker' : 'Manga Tracker'); |
|
32 | |||
33 | //We could just use global, but this is the only var we need in both header+footer |
||
34 | 17 | $this->footer_data['page'] = $this->header_data['page']; |
|
35 | |||
36 | 17 | $this->header_data['show_header'] = (array_key_exists('show_header', $this->header_data) ? $this->header_data['show_header'] : TRUE); |
|
37 | 17 | $this->footer_data['show_footer'] = (array_key_exists('show_footer', $this->footer_data) ? $this->footer_data['show_footer'] : TRUE); |
|
38 | |||
39 | 17 | $this->global_data['notices'] = get_notices(); |
|
40 | |||
41 | //$this->output->enable_profiler(TRUE); |
||
42 | |||
43 | 17 | $this->load->view('common/header', ($this->global_data + $this->header_data)); |
|
44 | 17 | foreach(func_get_args() as $path) { |
|
45 | 17 | view_exists($path) or show_404(); //CHECK: Does this have a performance impact? |
|
46 | |||
47 | 17 | $this->load->view($path, ($this->global_data + $this->body_data)); |
|
48 | } |
||
49 | //using the union operator + makes sure global_data always takes priority |
||
50 | //SEE: http://stackoverflow.com/a/2140094/1168377 |
||
51 | 17 | $this->load->view('common/footer', ($this->global_data + $this->footer_data)); |
|
52 | 17 | } |
|
53 | public function _render_json($json_input, bool $download = FALSE, string $filenamePrefix = 'tracker') : void { |
||
59 | public function _render_content(string $content, string $filenameExt, bool $download = FALSE, string $filenamePrefix = 'tracker') : void { |
||
60 | if($download) { |
||
74 | } |
||
75 | |||
147 |
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: