Storing PHP Object in session

General discussions related to php

Moderators: macek, egami, gesf

hkaren
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Jun 27, 2012 12:43 am

Storing PHP Object in session

Postby hkaren » Wed Jun 27, 2012 12:47 am

Hi all.
I am trying to execute the code in the below, but it returns nothing after second refresh of page. WHY?

Code: Select all

 session_start();

if(1==1){
     class A{
        public function getT(){
          return "ttext ttext ttext";
        }
     }
     if(isset($_SESSION['classA']))
       echo $_SESSION['classA']->getT();

     $_SESSION['classA'] = new A();
     echo $_SESSION['classA']->getT();
}


Thanks.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Storing PHP Object in session

Postby johnj » Wed Jun 27, 2012 5:30 am

Thought I am not an expert on this topic, my limited knowledge tells me that storing an object in a session variable is not a good practice for a variety of reasons.

But, you can try this: serialize the object before storing to session variable and unserialize it after you get it from the session. Read about serialize and unserialize from php.net.


Return to “PHP General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron