Page 1 of 1

Save class in session

Posted: Sun Aug 24, 2008 10:20 pm
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?

Re: Save class in session

Posted: Sun Aug 24, 2008 10:23 pm
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

Re: Save class in session

Posted: Sun Aug 24, 2008 10:34 pm
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.

Re: Save class in session

Posted: Mon Aug 25, 2008 1:17 pm
by soulcrazy
/bump

Re: Save class in session

Posted: Mon Aug 25, 2008 1:52 pm
by Christopher
soulcrazy wrote: all other pages are called from the index via QUERY_STRING.
What does this mean?

Re: Save class in session

Posted: Mon Aug 25, 2008 1:58 pm
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.

Re: Save class in session

Posted: Mon Aug 25, 2008 2:04 pm
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.

Re: Save class in session

Posted: Mon Aug 25, 2008 2:10 pm
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_

Re: Save class in session

Posted: Mon Aug 25, 2008 2:34 pm
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

Re: Save class in session

Posted: Mon Aug 25, 2008 2:41 pm
by Christopher
Works fine for me. What version of PHP are you using (I assume 5 from the code)?

Re: Save class in session

Posted: Mon Aug 25, 2008 3:05 pm
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

Re: Save class in session

Posted: Mon Aug 25, 2008 3:13 pm
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 :)

Re: Save class in session

Posted: Mon Aug 25, 2008 3:18 pm
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?

Re: Save class in session

Posted: Mon Aug 25, 2008 3:39 pm
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!

Re: Save class in session

Posted: Mon Aug 25, 2008 4:19 pm
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().