Jump to content

How can I keep my objects form deconstructing?


MrFish
 Share

Recommended Posts

I've got a login system that stores all the users information in objects. I serialize the objects and place them in sessions so I can use them through multiple pages. I want to make it so every time the user logs in and the object is constructed the user is added to an "online" table in the database. And every time it deconstructs it will remove that user. I've gotten the construction part to work but it seems that the object deconstructs at the end of the script on every page. But what is weired is that it only constructs once the first time, and even though it shows that is is "deconstructed", it will still hold the same variable values when I enter another page (and desconstruct yet again). So why doesn't my object run the destruct function once the script ends? Is there a way to prevent this or is there any other method to do what I'm trying to do?

Link to comment
Share on other sites

Try storing the unserialized object in the session instead. I believe you can do that as long as the class is defined before you use session_start, or else it won't get recreated as the right object type. If the object itself is stored in the session, and therefore there would be a reference to it, I don't think it will be destroyed. The destructor only runs once no more references to the object exist. Serializing an object to a string does not cause a reference to exist to the original object.

Link to comment
Share on other sites

I tried this:

$_SESSION[$name] = new Character($id);

and this:

$_SESSION[$name] = serialize(new Character($id));

and they both cause the same problem. But I also heard that storing objects in sessions was bad practice because it would be heavy on the server.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...