1

In my application, I am trying to save user information in a cache variable and access it in another method. I can access it inside the same method. But when I try to access out of that method, it returns null. I need to know how to achieve this, please someone help me out here with an example as I am new to the technology.

Inside a method:

Cache::set('user', $user);

Accessing it in another method:

$user = Cache::get('user');

And I want to clear it once accessing it.

  • use a class scope or singleton pattern for such problems. Cache is a completely different thing than what you are trying to do. – Anuj Shrestha Mar 18 '20 at 07:40

1 Answers1

0

Instead of using Cache for storing variable.

You can declare variable in class as following

class test{
    public $user = []; //declare your function here.

    function abc(){
        $this->user = $user; //Assign value to variable
    }

    function xyz(){
        $user = $this->user; 
        print_r($user);
    }

}
Harpal Singh
  • 694
  • 6
  • 27