0

What I want is, to achieve this method on my own like what i'm usually doing in laravel when accessing session.

I created interface which has a method of set and get just like what laravel does when accessing session. Then instantiate it in main class then access the get function like

    // This is my code     
    $session = new Session();
    $session->get('session_name')

I also tried it by using static function method like

    // This is also my code 
    $session::get('session_name')

Looks cool

behind in the session class

    class session implements sessionOperation()
    {
        public function set($string)
        {
            // code
        }

        public function get($string)
        {
            //code
        }

     }

But what I want to achieve is laravel like call

   // This is laravel code
   ​Session()->get('session_name')

It does'nt use a variable to call session. How to do that?

I know no matter how you write it still provides the same output. But I just wanna learn how to do it in my future reference too. Could someone help me?

Zen Zedon
  • 1
  • 1
  • Looks like `​Session()` is a function call, which presumably returns an object (which you can then call functions on). https://laravel.com/docs/9.x/session#the-global-session-helper – ADyson Mar 18 '22 at 10:43
  • Can you show me it in code if you don't mind? – Zen Zedon Mar 18 '22 at 10:48
  • Where are you stuck exactly? Declare a function. In the function, instantiate an object (of the type you're interested in). Then return the object from the function. – ADyson Mar 18 '22 at 10:57
  • I'm stuck at don't know how to do it. Thank you for the tip. – Zen Zedon Mar 18 '22 at 11:02
  • Well which bit are you stuck on exactly? I've just broken down the task into steps. Tried anything? – ADyson Mar 18 '22 at 11:12
  • This is the general idea: https://3v4l.org/k31CK – ADyson Mar 18 '22 at 11:26
  • I'm coding it all day. But i'm still stuck at the class that contains set and get function, which I called it in main class. Tried anything inside the function but still dont work. I created also a public function session and has function of set and get inside of it, and i don't know if im doing it right. – Zen Zedon Mar 18 '22 at 11:26
  • Well unless you show us what you've written and explain the specific problem we're not going to get very far (because "don't work" isn't a fixable issue, and we also cannot fix _descriptions_ of code). Please edit your question with your code, so we can be clear where your misunderstanding lies. – ADyson Mar 18 '22 at 11:30
  • Ah ok glad the demo helped. Did you need any more info? – ADyson Mar 18 '22 at 11:39
  • I apoligized about not showing my code behind the session class. I have just edited my question. – Zen Zedon Mar 18 '22 at 11:39
  • My post appeared duplicate. But I don't found any solution in stackoverflow. It would be a good reference in the future, how to remove duplicate status in my post? – Zen Zedon Mar 18 '22 at 11:41
  • The duplicate is quite close to what you're asking for really - it shows you how to return an object from a function, so that you can reference the object by calling the function and then run functions from that object directly. If you study carefully, it's quite similar to my demo. – ADyson Mar 18 '22 at 11:42
  • Your question can be re-opened if 3 people with sufficient privileges (or one person with a gold badge in one of the topics tagged in the question) vote for that to happen. But I'd say it's probably unlikely because it's a pretty reasonable duplicate. – ADyson Mar 18 '22 at 11:43

0 Answers0