Jump to content

How to Store Object in the session


kks_krishna

Recommended Posts

 

HI Friends,

 

How to store the whole object into session?

For example, When user login I want to fetch all the details related to user and put into session with single object. And also how to set the values into object. I am very new to PHP object programming. Please help me.

Link to comment
https://forums.phpfreaks.com/topic/59558-how-to-store-object-in-the-session/
Share on other sites

For example, In Java we called its as Beans or ValueObjects to store the values like:

 

Class Test
{
private firstName;
private lastName;

public void setFirstName(String firstName)
{
  this.firstName = firstName;
}
public String getName()
{
    return this.firstName
}
// all the methods

}

 

 

Like this can we do in PHP. or any other way?

we store data in php like

$variable='your value'; and the data type is predetermine by php unlike in java we declare like int or string and the values or variable dont have the $ sign on it  now if you talk about object in php we use classes like java do

$_SESSION isn't a normal object.  It's more of a super global array that transcends page loads for the user (for a period of time).  You can create objects from classes in php just the same as java, just refer to the docs for syntax(http://www.php.net/oop5), you can then serialize those objects (http://www.php.net/serialize) and store them in $_SESSION, then on the next page load, unserialize (http://www.php.net/unserialize) the object and use it like before.

 

There are some exceptions...database connections are per page load...they do not store in the session at all.  But most other things will.

 

If you are doing any OOP with php, make sure you are using php5 if at all possible.

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.