首頁 >php教程 >PHP源码 >簡單的SESSION管理類

簡單的SESSION管理類

大家讲道理
大家讲道理原創
2016-11-09 09:54:321142瀏覽

一個很基礎的單例模式的SESSION管理類別。

<?php
class session {
    const SESSION_STARTED = TRUE;
    const SESSION_NOT_STARTED = FALSE;
 
    private $session_state = self::SESSION_NOT_STARTED;
    private static $instance;
 
    public static function get_instance(){
        if(!isset(self::$instance)){
            self::$instance = new self;
        }
 
        self::$instance->start_session();
        return self::$instance;
    }
 
    public function start_session(){
        if($this->session_state == self::SESSION_NOT_STARTED){
            $this->session_state = session_start();
        }
        return $this->session_state;
    }
 
    public function __set($name,$value){
        $_SESSION[$name] = $value;
    }
 
    public function __get($name){
        if(isset($_SESSION[$name])){
            return $_SESSION[$name];
        }
    }
 
    public function __isset($name){
        return isset($_SESSION[$name]);
    }
 
    public function __unsset($name){
        unset($_SESSION[$name]);
    }
 
    public function destroy(){
        if($this->session_state == self::SESSION_STARTED){
            $this->session_state = !session_destroy();
            unset($_SESSION);
            return !$this->session_state;
        }
 
        return false;
    }
}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn