Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Storing PHP Object in session

General discussions related to php

Moderators: macek, egami, gesf

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.
hkaren
New php-forum User
New php-forum User
 
Posts: 1
Joined: Wed Jun 27, 2012 12:43 am

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.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron