![]() |
intController() and __construct() - 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: intController() and __construct() (/showthread.php?tid=71302) |
intController() and __construct() - enlivenapp - 07-30-2018 I'd like to make sure I have my head wrapped around the removal no longer need of __contruct() from controllers to construct the parent. I I've got that PHP Code: public function __construct() specifically is no longer needed as intController() takes care of all that. So, where I'm a bit fuzzy is this: When I need/want to initialize a controller wide access to a resource, IE: (shortened for brevity), The "old way" PHP Code: <?php namespace App\Controllers; Is this still valid to set up a controller-wide available resource? or should we be using these in each method when we need them and not use __construct() at all? IE: PHP Code: <?php namespace App\Controllers; I realize using database and builder is open to 'the fat/skinny controller/model argument', this is just an example to ask my question... RE: intController() and __consturct() - kilishan - 07-30-2018 You're overthinking it ![]() Yes - it's perfectly valid to do controller-wide resources. Just forget the initController() exists and you'll be fine. That's used by the framework, not by you. It just makes it a little simpler/cleaner when you want to create controller-wide resources. Instead of: Code: public function__construct(...$params) You would do: Code: public function__construct() RE: intController() and __consturct() - enlivenapp - 07-30-2018 (07-30-2018, 02:13 PM)kilishan Wrote: You're overthinking itIt's how I roll. ![]() Thanks! RE: intController() and __consturct() - kilishan - 07-30-2018 (07-30-2018, 02:29 PM)enlivenapp Wrote:(07-30-2018, 02:13 PM)kilishan Wrote: You're overthinking itIt's how I roll. I've been known to do that myself, that's how I can recognize it in you lol. RE: intController() and __construct() - dgvirtual - 05-22-2022 I have just noticed that I cannot access the variables initiated in BaseController initController method in the constructors of my controllers... I have $this->settingsModel = model('SettingsModel'); $this->settings = $this->settingsModel->keyValue(); in my BaseController initController method, and running $hl = explode('-', $this->settings['booking_hours']); in controller methods goes fine, but if I do this in function __construct() I get an error Undefined property: App\Controllers\Booking::$settings What would be the correct way to do things here? Maybe use initController with a parent call in my controller classes instead of __construct()? I could, of course, simply initiate the needed model and run the $this->settings = $this->settingsModel->keyValue(); in the constructor... RE: intController() and __construct() - kenjis - 05-22-2022 @dgvirtual `initController()` is called after calling `__construct()`. `__construct()` is PHP language method that is called (by PHP) when creating an instance. After instantiation of a controller, CI4 calls `initController()`. RE: intController() and __construct() - dgvirtual - 05-22-2022 (05-22-2022, 02:43 AM)kenjis Wrote: @dgvirtual `initController()` is called after calling `__construct()`. Yep, I could deduce that. so, could I replace my constructors with an initController() method? do it like it is done in the BaseController init method: call the method and then call it's parent inside? PHP Code: parent::initController($request, $response, $logger); |