通过对象分配会话值

时间:2014-06-09 23:19:54

标签: php oop session

我一直在尝试在对象中分配/更改/更新会话值。

我要做的是创建会话并以对象格式分配值,并更新其调用/启动对象的值。

<?php
    class Session {
        var $namespace; //NULL by Default
    var $session;
    //run session essantials
    public function __construct($SessionName=null, $assignedValue=null, $boolenerOfType=NULL){
        $this->namespace = (!empty($SessionName))?$SessionName:NULL;
            if(!empty($_SESSION[$SessionName])){
            if(!empty($assignedValue)){
                    //this overwrites the existing value of the session
                        $_SESSION[$SessionName] = $assignedValue? $assignedValue : new stdClass;

                    }else{//end of if
                        $_SESSION[$SessionName] = (!empty($_SESSION[$SessionName])==true)? $_SESSION[$SessionName] : new stdClass;
                    }

            }elseif(!isset($_SESSION[$SessionName]) or empty($_SESSION[$SessionName])){//end of if

                    //$_SESSION[$SessionName] = ($assignedValue==NULL)? (new stdClass): function($assignedValue){   foreach($assignedValue as $val){}};
                    $_SESSION[$SessionName]== new stdClass;
            }//end of else of if

        $this->session = $_SESSION[$SessionName];



        foreach($this->session as $num=>$val){
            $this->$num=$val;
        }

        foreach($_SESSION[$SessionName] as $num=>$val){
            $this->$num=$val;
        }
        //debug::dump($this);
        unset($this->session);

        return $this;   

    }//end of function



    static public function unsetSession($sessionName){
        unset($_SESSION[$sessionName]);
        return true;
    }//end of function
    public function setSessionValue($valueObject, $obj){
        $SessionName = $obj->namespace;
        $_SESSION[$SessionName]=$valueObject;
    }//end of function


    public function __destruct(){
        //$reassignSession = $this->session;
        $this->__construct($this->namespace);
        $SessionName = $this->namespace;
        //debug::dump($_SESSION);
        $methodsofClass = (get_class_methods ($this ));
        $valueObject = new stdClass;

        foreach($this as $id=> $value){
            if(!in_array($id, $methodsofClass) and $id!= 'namespace' and $id!= 'session'){
                $valueObject->$id=$value;
            }//end of if
        }//end of foreach
        foreach($_SESSION[$SessionName] as $num=>$val){
            $this->$num=$val;
        }
        $this->setSessionValue($valueObject, $this);

        return @$this->session;
    }//end of function
  }//end of class

        $session = new Session('somename');
        $session->var1 = 1;
        $session->var2 = 'some value';

        //and my session ends like this

        /*
         $_SESSION['somename'] = object(Session)#n {
          var1 => 1,
          var2=> 'some value'

          }



        */
         ?>

我的代码现在有错误,因为我一直在尝试 我正在努力创造逻辑 我怎么能这样做?
我尝试使用__desctruct(),但我无法使其工作     我一直在尝试这么多东西,实际上我已经迷失了自己的道路     提前致谢

0 个答案:

没有答案