Save class in session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Save class in session

Post by soulcrazy »

I've been trying to find a way to hold a class object in a session.

Code: Select all

 
# at start of every page:
        require_once('cls/ClassDO.class.php');     // ClassDO def.
    session_start();
 
        if ( is_object( $_SESSION['classDO_'] ) ) {
        $classDO = $_SESSION['classDO_'];
        //$classDO = unserialize($_SESSION['classDO_']);   # unserialize only works for strings!? [A]
    } else {
        $classDO = new ClassDO();
    } # end if
 
/*     html, etc here!
        */
 
// bottom of the page:
        $_SESSION['classDO_'] = $classDO; 
       // $_SESSION['classDO_'] = serialize( $classDO );    # removed because of [A]
 
A few pages later into the session, I try to save info to the class:

Code: Select all

 
#this code would go where "html, etc here!" is (above)...
$classDO->setData($a, $b, $c);
 
This throws an error:
Fatal error: Call to a member function setData() on a non-object in /path...


Any ideas how I can hold an entire class in a php session? or is there a better way?
Last edited by soulcrazy on Sun Aug 24, 2008 10:37 pm, edited 2 times in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Save class in session

Post by Christopher »

Are you sure you do this every page?

Code: Select all

# at start of every page:
    require_once('cls/ClassDO.class.php');     // ClassDO def.
    session_start();
 
    if ( is_object( $_SESSION['classDO_'] ) ) {
        $classDO = $_SESSION['classDO_'];
    } else {
        $classDO = new ClassDO();
    } # end if
(#10850)
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Save class in session

Post by soulcrazy »

absolutely..it's in the index and all other pages are called from the index via QUERY_STRING.

do i have to set something in php.ini?

I've searched google for hours looking for alternative solutions but nothing works...The only solution I have found is:
http://www.phpclasses.org/browse/package/3768.html

Although, I have not attempted implementation of that yet.
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Save class in session

Post by soulcrazy »

/bump
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Save class in session

Post by Christopher »

soulcrazy wrote: all other pages are called from the index via QUERY_STRING.
What does this mean?
(#10850)
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Save class in session

Post by soulcrazy »

$_GET or $_SERVER['QUERY_STRING']

.. I meant to say $_GET.

Anyways, that has nothing to do with the problem at hand.

The code is how it is in my first post. Let's stay on topic.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Save class in session

Post by Christopher »

It is not clear from your statement or code that you are including the correct code for each request. Can you post code that can be actually run. The above example is not working code.
(#10850)
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Save class in session

Post by soulcrazy »

arborint wrote:It is not clear from your statement or code that you are including the correct code for each request. Can you post code that can be actually run. The above example is not working code.

Code: Select all

 
require_once('cls/ClassDO.class.php');     // ClassDO def.
session_start();
  
if ( is_object( $_SESSION['classDO_'] ) ) {
    $classDO = $_SESSION['classDO_'];
} else {
    $classDO = new ClassDO();
} # end if
 
$a = 0;
$classDO->setData($a);
 
$_SESSION['classDO_'] = $classDO; 
 
FILE: ClassDO.class.php

Code: Select all

 
class ClassDO {
    private $a_;
    
    public function _construct() {}
    public function setData($a) { $this->a_ = $a; }
}
 
ERROR: Fatal error: Call to a member function setData() on a non-object in _PATH_
Last edited by soulcrazy on Mon Aug 25, 2008 3:14 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Save class in session

Post by RobertGonzalez »

Have a look at the magic methods __sleep and __wakeup at this PHP Manual page --> http://us3.php.net/manual/en/language.oop5.magic.php
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Save class in session

Post by Christopher »

Works fine for me. What version of PHP are you using (I assume 5 from the code)?
(#10850)
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Save class in session

Post by soulcrazy »

arborint wrote:Works fine for me. What version of PHP are you using (I assume 5 from the code)?
PHP Version 5.2.4-2ubuntu5.1
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Save class in session

Post by soulcrazy »

Everah wrote:Have a look at the magic methods __sleep and __wakeup at this PHP Manual page --> http://us3.php.net/manual/en/language.oop5.magic.php

Thanks.

I'll read this later tonight :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Save class in session

Post by RobertGonzalez »

This is working for me too. Can you use confirm your error reporting levels are cranked up all the way before doing this?
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Save class in session

Post by soulcrazy »

I figured it out...the variable ($classDO) was out of scope.

Thanks to all who contributed.

Here is the correct code (I simplified it, removed $classDO var)...

Code: Select all

 
        if ( !is_object($_SESSION['classDO_']) )
        $_SESSION['classDO_'] = new ClassDO();
 
I can now call $_SESSION['classDO_']->setData();

KISS - Keep It Simple, Stupid :)

Hopefully this will be of some use to others considering I could not find any documentation on storing objects in session vars...Anywhere!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Save class in session

Post by RobertGonzalez »

Something I ran into when testing your code is that you do not check isset() on that session var so the first time your code runs you get an undefined index error on that session var. You might want to add an isset() check before checking is_object().
Post Reply