CodeIgniter Forums
Error when accessing base controller variable - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Error when accessing base controller variable (/thread-76885.html)



Error when accessing base controller variable - campingrhino - 06-29-2020

Hi there, I'm hoping someone can help. I've got a class that I'm using in my controllers and libraries to handle settings that are stored in the database. When accessing them in my controller via $this->settings->get('blah'), I have no problems at all.

However, when I pass $this->settings in the controller's __construct() to my template library, it's passing a null value and I get this error:

Quote:TypeError

Argument 1 passed to MyApp\TemplateRenderer::__construct() must be an instance of MyApp\Settings, null given, called in



I've loaded the library in the base controller as shown below.

This is my BaseController:

PHP Code:
    public $session$settings;

    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        $this->session session();
        $this->settings = new Settings();
    } 


This is the Settings class:

PHP Code:
class Settings {
    
    
// A container for all of the settings values.
    protected $settings = [];

    public function __construct()
    {
        $settings_model = new SettingsModel();

        $settings $settings_model->findAll();

        foreach($settings as $setting) {
            if($setting['value'] == 'true' || $setting['value'] == 'false') {
                $this->settings[$setting['key']] = ($setting['value'] == 'true' true false);
            } else {
                $this->settings[$setting['key']] = $setting['value'];
            }
        }
    }

    public function get($key)
    {
        if(key_exists($key$this->settings)) {
            return $this->settings[$key];
        } else {
            return false;
        }
    

And finally my controller itself:

PHP Code:
class AccountController extends BaseController
{
    public function __construct()
    {
        $this->template = new TemplateRenderer($this->settings);
    

Any help would be greatly appreciated! Thanks.


RE: Error when accessing base controller variable - captain-sensible - 07-02-2020

this is how i'm setting some BaseController , class member properties

public function __construct()
{
helper(['text', 'date']);

$this->theTime = now('Australia/Victoria');
$this->theDate = date("d/m/Y",$this->theTime);
}


RE: Error when accessing base controller variable - ojmichael - 07-02-2020

Your issue is the class constructor will run before initController method. Use this instead.

PHP Code:
class AccountController extends BaseController
{
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);

        $this->template = new TemplateRenderer($this->settings);