CodeIgniter Forums
Constructor - Assigning class variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Constructor - Assigning class variables (/showthread.php?tid=76422)



Constructor - Assigning class variables - nmaa3003 - 05-11-2020

Code:
class Home extends BaseController
{
    var $cache;

    public function __contruct()
    {
        parent::__construct();
        $this->cache = \Config\Services::cache();
    }
    public function reset()
    {
        $this->cache->clean();
    }
}

assume i have create a web cache.

the idea is i dont want to keep assign the same $cache in every method.

So contructor should do the job.

but when i load the page, it shows -> Undefined variable: cache

how to fix my code/this issue?


RE: ci4 constructor not working - jreklund - 05-11-2020

You should not named it "var $cache;". It should be "protected $cache;".