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
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$_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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.