Unable to access variable - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Unable to access variable (/showthread.php?tid=81613) |
Unable to access variable - spreaderman - 03-26-2022 I have a BaseController which initiates some variables as follows; public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); $this->global_settings = $this->loadGlobalSettings(); $global_settings_cache = cache()->get('global_settings'); $this->data['dh_site_theme'] = $global_settings_cache['dh_site_theme']; } I then have UserPublic_Controller as follows: namespace App\Controllers\User_public; use App\Controllers\User_Public\UserPublic_Controller; use App\Controllers\BaseController; use App\Libraries\Theme; class UserPublic_Controller extends BaseController{ protected function __construct(){ $this->data['user_public_css'] .= $dh_site_theme->theme_cdn($dh_site_theme); } I cannot understand why I cannot access $dh_site_theme ... it shows null. Even in the VIEW debug bar I can see it is set to "journal", one of the seems I have. VIEW DATA / Debug bar. dh_site_theme journal Any help appreciated. RE: Unable to access variable - kenjis - 03-26-2022 The local variable $dh_site_theme is not defined in your code. Why do you think it has a value? RE: Unable to access variable - iRedds - 03-26-2022 start learning PHP =) RE: Unable to access variable - spreaderman - 03-26-2022 This is defined in the basecontroller; $this->data['dh_site_theme'] = $global_settings_cache['dh_site_theme']; $data is defined as: protected $data = []; in the BaseController. UserPublic_Controller extends BaseController To access $data should be $this->data['dh_site_theme'] I thought (sorry, copied the wrong script below) but it turns out null. RE: Unable to access variable - vitnibel - 03-27-2022 Hi it happens because constructor of UserPublic_Controller class called before initController of BaseController executed. or in other words first the constructor is called and then the function initController inherited from BaseController RE: Unable to access variable - spreaderman - 03-27-2022 @ vitnibel thanks. Did not realize that. I thought initController was more or less working as a __constructor. Is there any way to add a __constructor to initController? I am creating my cache in the BaseController which is a bunch of settings for my website. They are mainly required in the view so that works ok but I create my CSS, JS, etc, in the __construct part of my Public_Controller and need to know the cache setting then. I would like to keep the reading of cache in the base controller and have it loaded it first. Any ideas? I guess one solution is (1) insert below method into initController Code: public function loadGlobalSettings() Code: class Public_Controller extends BaseController{ Something like above is ok? RE: Unable to access variable - vitnibel - 03-27-2022 everything is easier. you just need to place the initialization code of the UserPublic_Controller class in the initController function and call the base class function of the same name in the first line. class Public_Controller extends BaseController { public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); $this->data['user_public_css'] .= $dh_site_theme->theme_cdn($dh_site_theme); } } RE: Unable to access variable - spreaderman - 03-27-2022 Thank you very much. All very clear. Much appreciated!! |