Re: adding objects to $SESSION with serialize()

From: Date: Wed, 23 Mar 2011 21:16:48 +0000
Subject: Re: adding objects to $SESSION with serialize()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-312022@lists.php.net to get a copy of this message
Daniele Capuano wrote:
Hi, I'm developing a web application using moodle, and I'm trying to create a PHP object tree to be used in $SESSION. Objects are defined as class foo { private $module_name; private $sub_modules = array(); } I have a main module (object) and I use the following function to add serialized sub modules to such object: public function add_sub_module(&$mod) {
    $name = $mod->get_mod_name();
    $this->sub_modules[$name] = serialize(&$mod);
} where the get_mod_name() function simply returns the $module_name private field. After adding a sub module to the main module, I always update the main module in $SESSION using serialize($main_module). Once returned to the page, i restore the main module with unserialize($SESSION->$main_module_name), and then I call the following function to retrieve a sub module: public function&search_sub_module($name='') {;
        foreach($this->sub_modules as $mod_name =>  $mod) {
           if ($name == $mod_name) {
              return unserialize($mod);
           }
           $obj_mod_file = $mod_name.".php";
           require_once($obj_mod_file);
           $obj_mod = unserialize($mod);
           $modr =&  $obj_mod->search_sub_module($name);
           if ($modr != NULL) {
               return $modr;
           }
        }
        return NULL;
} I found that sub_modules added to the main_module->sub_modules list are correctly retrieved, but if I add a sub module to a main_module's sub module, it cannot be get after the main module has been serialized. What do I mistake? Please help. Thanks Daniele
Is '$SESSION' just a typo?.. s/b '$_SESSION'. Donovan -- D Brooke

« previous php.general (#312022) next »