views:

105

answers:

3

With PHP and MySQL I am working on a user login type site. Here is my plan so far.

User logs in with email/password I then set a few things into session (UserID, photoURL, userName, latitude, longitude, gender) These are the basic items that will be saved into session and possibly APC/Memcache when a user is "logged in".

I have my session handling and cache handling wrapped in Classes so there is a user object which is getting the data on each page load by creating a session and cache object as well and then importing those objects into a main User object.

Now I am confused, I have always done it this way or similar but I know that some people serialize there objects and then save them to session or cache and then retrieve the object on page load. Would it be better to do it that way?

+1  A: 

I don't think that the all-in-session way is the best way, it's only another way. If you store serialized objects in the session and then deserialize them it will be certainly faster, but you must store a large amount of data. Personally i prefer the object regeneration on every page load and i use the session serialization only when the creation of the object is very slow.

mck89
Ok thanks, I wasn't sure if it was faster or slower, I thought serializing and un would be a heavy opperation
jasondavis
+1  A: 

i think you don't have to think about it. I work as a php developer at my company where we have a webpage with literally hundreads of objects created every time a page loads and it's serving pages to like 50-200 people at a time and it takes just about 50ms to generate the page. So if you realy don't need the extra speed gained from serializing objects then you don't have to worry about it.

Gabriel
I am new to using objects so I really don't know anything about there performance yet, so in your last sentence are you saying that serializing an object and passing it page to page IS faster?
jasondavis
i just think serialization is faster. but if you have just some simple objects (what seems to be the case), you can just create your objects every time.As i said, you're just better off not serializing. Unless you're using very complex objects that take like 100-200ms to create.
Gabriel
+1  A: 

Sounds like premature optimization.

Historically, it was certainly a good idea to store objects as serialized entities - as soon as you call session_start and retrieve an existing session, the engine would have to restore the objects - potentially before the class definitions were processed. However now de-serialization of session objects can be defered.

As long as your user object is not horrendously complicated (it doesn't appear to be) then doing anything other than storing the object in the session is probably overkill (If your user class is very complex you might want to think about splitting it down - do make sure that you authenticator and authorizer are separate entitles).

Note that the session object transparently serializes and unserializes all of its contents - again you may see some benefit by making good use of the __sleep and __wakeup methods.

HTH

C.

symcbean