Jump to content

[SOLVED] How to remember an object?


freebsdntu

Recommended Posts

I have created a user class for user,in the class, there are profile attributes like names,email,etc and I also have get and set method for each of the attribute. Besides those, there are also user action methods like browseProfile,editProfile,etc

When a user log in,I will instantiate a user object,the problem is how to remember the object so that other php files can also use it?I have tried something like this:$_SESSION['user'] = $user, where the $user is the object.But id did not work. Does anybody have any suggestion on how to do this?

Thank you very much!

Link to comment
https://forums.phpfreaks.com/topic/82014-solved-how-to-remember-an-object/
Share on other sites

Thank you for your reply,rajivgonsalves .I tried your method, but got this:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

I remember I read somewhere that session_start() should always be the first statement,I don't know whether it is due to this reason.

Also,does that mean I must include the file in every php file that I am gonna use the object?

Oh,sorry,I got it wrong,that warning came because I output something before calling session_start() in the file before redirection.

But another problem I discovered is that if I invoke methods like $user -> getUsername(), it returns empty string, is this the problem with the $user object?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.