TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   storing class instance into session (http://www.talkphp.com/advanced-php-programming/3407-storing-class-instance-into-session.html)

nizzy 10-02-2008 02:50 AM

storing class instance into session
 
Do you think SetObject() method will work?


PHP Code:

class Session
{
    ...........
    ...........

    public function 
SetObject($class_name)
    {
        if(!isset(
$_SESSION[$class_name]))
        {
            
$num_args func_num_args();
            
$params = array();

            if(
$num_args == 1)
            {
                
$obj_ins = new $class_name();
            }
            elseif(
$num_args 1)
            {
                
$get_args func_get_args();
                
$params array_shift($get_args);

                
$obj_ins call_user_func_array($class_name$params);
                
//$obj_ins = call_user_func_array(array($this, 'parent::__construct'), $params); // not sure about this one
            
}

            
$_SESSION[$class_name] = serialize(new $obj_ins());
        }
    }

    public function 
GetObject($class_name)
    {
        if(isset(
$_SESSION[$class_name]))
        {
            
$instance unserialize($_SESSION[$class_name]);

            if(
$instance instanceof $class_name)
            {
                return 
$instance;
            }
        }
    }
}

$sess_handler =& new Session();

//storing class instance into session
$sess_handler->SetObject('FooBar'$arg_1$arg_2$arg_3....etc);

//getting class instance from session
$foo_bar $sess_handler->GetObject('FooBar');

$foo_bar->print_hello(); // this is a method in FooBar class 


Kalle 10-02-2008 03:49 AM

Its possible to serialize object instances and store them in session variables, you just have to remember to include the class declaring before the session_start() call, else deserialized instances will become an instance of __PHP_Incomplete_Class.

nizzy 10-02-2008 04:01 AM

yes, before session
 
Dear Kalle

Thanks for quick reply. Then my method will not work, because I am trying to store the FooBar class into $_SESSION var AFTER the session is created. I see this is the problem.

Now, how about this? is this the right way of applying the params to a CLASS (not a function)

$obj_ins = call_user_func_array($class_name, $params);

thank you

nizzy 10-02-2008 06:49 AM

I found my own solution
 
YES! you can store object instance into session AFTER session initialization.

THE WHOLE trick resides in these lines (requires php v5.1.3):
$reflection_obj = new ReflectionClass($class_name);
$obj_ins = $reflection_obj->newInstanceArgs($get_args);


PHP Code:

<?php

class FooBar
{
    public 
$arg_1;
    public 
$arg_2;
    public 
$arg_3;

    public function 
__construct($arg_1$arg_2$arg_3)
    {
        
$this->arg_1 $arg_1;
        
$this->arg_2 $arg_2;
        
$this->arg_3 $arg_3;
    }

    public function 
hello()
    {
        return 
"hello from __CLASS__, arg_1: " $this->arg_1 ", arg_2: " print_r($this->arg_2true) . ", arg_3: " $this->arg_3;
    }
}

class 
Session
{
    public function 
__construct()
    {
        
session_start(); 
    }

    public function 
SetObject($class_name)
    {
        unset(
$_SESSION[$class_name]); 

        if(!isset(
$_SESSION[$class_name]))
        {
            
$num_args func_num_args();
            
$params = array();

            if(
$num_args == 1)
            {
                
$obj_ins = new $class_name();
            }
            elseif(
$num_args 1)
            {
                
$get_args func_get_args();
                
array_shift($get_args);

                
$reflection_obj = new ReflectionClass($class_name); 
                
$obj_ins $reflection_obj->newInstanceArgs($get_args); 
            }

            
$_SESSION[$class_name] = serialize($obj_ins);
        }
    }

    public function 
GetObject($class_name)
    {
        if(isset(
$_SESSION[$class_name]))
        {
            
$instance unserialize($_SESSION[$class_name]);

            if(
$instance instanceof $class_name)
            {
                return 
$instance;
            }
        }
    }
}

$sess_handler =& new Session();

//storing class instance into session
$sess_handler->SetObject('FooBar''first_str', array('hey''budy'), true);

//getting class instance from session
$foo_bar $sess_handler->GetObject('FooBar');

//var_dump($foo_bar);

echo $foo_bar->hello(); // this is a method in FooBar class 

?>



All times are GMT. The time now is 09:21 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0