I have a class named user. I create an instance of that class to be able to deal with it. This class has a function called login, which does exactly that. It logged the user in. I call it like this:
$user = new user();
$user -> logIn();
Later in a different file I want to be able to do something with the object again. My first thought was "Hey make it global and live a happy life", but then it occurred to me, that now if a second user comes along and logs in, they would be using the same object - resulting in chaos. Since I'm learning Java in University, I then thought about using a HashMap-styled Array, to save the user's session id and his object. In PHP it would look like this:
$users = array[ session_id1 => user1object,
session_id2 => user2object
]
With the session_id I would be able to track the user's object at any time (well at least as long as he is logged in). Now my question: Is that the best way - am I overlooking something?
Now back one step: Assuming that is the way to go, I have another problem: I'm creating the object, calling its login-function. Now if the user logged in successfully, I'm in the login-function within the users object. How do I store that object into my array (e.g. HashMap-style).
Thanks a lot, stiller_leser
I found those two questions, but they did not answer my own question: oop - How does PHP track created objects PHP, OOP, Creating an object (closed)