View Single Post
Old 10-02-2008, 02:50 AM   #1 (permalink)
nizzy
The Visitor
 
Join Date: Oct 2008
Posts: 3
Thanks: 0
nizzy is on a distinguished road
Default 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 
nizzy is offline  
Reply With Quote