• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Constructor - Assigning class variables

#1
Question 
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?
Reply

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme ¬© 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.