-4

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)

Community
  • 1
  • 1
stiller_leser
  • 1,512
  • 1
  • 16
  • 39
  • When would you have two users logged in simultaneously? – Waleed Khan Apr 02 '13 at 16:06
  • The first question you showed is clearly answered: `Session` does the hard work. You pass an object and `Session` serializes it, so you move between requests. PHP doesn't have anything called persistence (which is why sessions and cookies exist) – Sergi Juanola Apr 02 '13 at 16:09
  • I thoguht that since PHP is on the Server side, there could be more than one user on my site - meaning that more than one user would be logged in. Or are the php-objects of those two users already independent from each other? – stiller_leser Apr 02 '13 at 16:10
  • 4
    You're not understanding PHP properly. Each 'hit' on a webserver is a handled by a completely independent instance of PHP. Two users hitting the same script on the same server at the same time cannot "collide". The actual PHP instances will have seperate memory spaces. – Marc B Apr 02 '13 at 16:10
  • Well, @MarcB is right. Two users execute different instances of PHP. However, once in a lifetime, [they can collide](http://stackoverflow.com/questions/138670/how-unique-is-the-php-session-id) (although I've never seen such) – Sergi Juanola Apr 02 '13 at 16:12
  • 1
    @Korcholis: that's a session ID. they may share a session, but would never EVER share the same instance of PHP. – Marc B Apr 02 '13 at 16:12
  • The question has nothing to do with *objects* but with parallel requests. Objects are handled the same way as any other variable. Don't confuse PHP with Java. – deceze Apr 02 '13 at 16:14
  • Thanks for clearing that up. Guess I'll do some additional reading. However: I do understand that orthography is important, but since English is not my native language, it is pretty harsh to downvote my question for wrong spelling. Anyway thanks for the answers. – stiller_leser Apr 02 '13 at 16:16
  • Nobody said your question was downvoted for *wrong spelling*. :) – deceze Apr 02 '13 at 16:19
  • @MarcB something related to what @deceze said. You work with objects, but at some time, you may want to _store_ the information. PHP executions are independent, but what I understood (specially by looking at `session_id1=>...` HashMap he designed), he was thinking about sessions, and not executions per se. Anyway, yours was a good point. – Sergi Juanola Apr 02 '13 at 16:21

2 Answers2

3

This assumption is wrong:

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.

Different users will NOT access the same object. So to answer your question: No this is not the best way - you don't need to make an array to keep track of different users' objects...

darwin
  • 211
  • 3
  • 15
0

When two users run the same script on a server, the variables will not interfere. That is because memory for variables is only being allocated for the duration of the execution of the script. Everytime a user calls a PHP script, the server allocates new memory and after the script has been executed, the memory is freed again.

That means, that two users calling the same script will have different user objects, but if one user calls the script twice, he will also have two different user objects, since the server allocates two different "sets" of memory and handles them independently for each script execution.

If you want to achieve that the user objects will stay the same between script calls and every user has his own user object, then you should look into sessions and optionally serialization. Sessions roughly work in this way:

When a user visits the server the first time, he is given a unique identifier and a file on the server is being created. The file holds the session variables. In these session variables, you can store whatever you want (e.g. your user objects).

The next time the user calls the script, the variables inside that file are loaded again and you can reuse them.

Butt4cak3
  • 2,389
  • 15
  • 15