Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 类封装$\u会话-问题_Php_Session Variables - Fatal编程技术网

Php 类封装$\u会话-问题

Php 类封装$\u会话-问题,php,session-variables,Php,Session Variables,可能重复: include/session.php: /* Use the static method getInstance to get the object. */ class Session { const SESSION_STARTED = TRUE; const SESSION_NOT_STARTED = FALSE; // The state of the session private $sessionState = self::SE

可能重复:

include/session.php:
/*
    Use the static method getInstance to get the object.
*/

class Session
{
    const SESSION_STARTED = TRUE;
    const SESSION_NOT_STARTED = FALSE;

    // The state of the session
    private $sessionState = self::SESSION_NOT_STARTED;

    // THE only instance of the class
    private static $instance;

    protected function __construct() { }

    public function __destruct() {
      session_write_close();
    }


    /**
    *    Returns THE instance of 'Session'.
    *    The session is automatically initialized if it wasn't.
    *    
    *    @return    object
    **/

    public static function GetInstance()
    {
        if ( !isset(self::$instance))
        {
            self::$instance = new self;
        }

        self::$instance->startSession();

        return self::$instance;
    }

    public function getID() {
      return session_id();
    }


    /**
    *    (Re)starts the session.
    *    
    *    @return    bool    TRUE if the session has been initialized, else FALSE.
    **/

    public function startSession()
    {
        if ( $this->sessionState == self::SESSION_NOT_STARTED )
        {
            $this->sessionState = session_start();
        }

        return $this->sessionState;
    }


    /**
     *    Creates a new session.
     **/         
    public function newSession() {
        return session_regenerate_id(true);
    }


    /**
    *    Stores datas in the session.
    *    Example: $instance->foo = 'bar';
    *    
    *    @param    name    Name of the datas.
    *    @param    value    Your datas.
    *    @return    void
    **/

    public function __set( $name , $value )
    {
        $_SESSION[$name] = $value;
    }


    /**
    *    Gets datas from the session.
    *    Example: echo $instance->foo;
    *    
    *    @param    name    Name of the datas to get.
    *    @return    mixed    Datas stored in session.
    **/

    public function __get( $name )
    {
        if ( isset($_SESSION[$name]))
        {
            $ret = $_SESSION[$name];
            return $ret;
        }
    }


    public function __isset( $name )
    {
        return isset($_SESSION[$name]);
    }


    public function __unset( $name )
    {
        unset( $_SESSION[$name] );
    }


    /**
    *    Destroys the current session.
    *    
    *    @return    bool    TRUE is session has been deleted, else FALSE.
    **/

    public function destroy()
    {    
      session_start();
      session_unset();
      session_destroy();
    }
}

?>
test.php:

<?php

require_once('include/session.php');

$session = Session::GetInstance();
$session->foo = 'bar';
$session->baz = array();
$session->baz['foo'] = 'bar';
$session->baz['derp'] = array();
$session->baz['derp']['php_sucks'] = 'this will never work';

var_dump($session->foo); echo '<br>';
var_dump($session->baz); echo '<br>';
var_dump($session->baz['foo']); echo '<br>';
var_dump($session->baz['derp']); echo '<br>';
var_dump($session->baz['derp']['php_sucks']); echo '<br>';


?>

为什么$session->baz数组没有被填充?

当调用$session->baz['foo']时,$session->baz返回会话中数组的副本,然后向其中添加'foo'元素。这个副本不是你的类中的副本,并且几乎会立即被丢弃


您需要改变处理数组的方式,即使用一些getter和setter(包括神奇的getter和setter)或者研究通过引用而不是复制来获取“baz”元素的方法。

-1用于免费的单例,以及用一个不提供任何有用功能的类包装$\u会话。@Andre它不在那里,
\u set
\u get
方法就是为了这个目的。@cHao,它可能是免费的,但它是有用的,因为在很多情况下,您需要使用会话变量,但不确定是否(尚未)调用了
session\u start
。通过使用单例,您不必担心多次调用它。另一种解决方案是了解您的程序在做什么。此外,我没有看到在从
\uu get
返回内容之前是否已启动会话的检查。因此,您刚刚添加了semi-required,但是您可以忘记对
Session::GetInstance
的调用(这也很容易被忘记,而且只会“起作用”)由于它被称为其他5000万个会话变量中的一个。一点重构,一点忘记启动会话,突然有一天,应用程序中途的编辑中断了会话。Andre,baz是一个会话变量……看看会话覆盖的神奇方法;$this->baz应该设置$_会话['baz']赵先生……你是个白痴,我不希望存在两个实例同时试图分别管理会话的可能性,而且我也没有要求对基本设计模式发表毫无价值和迂腐的评论。
string(3) "bar" 
array(0) { } 
NULL 
NULL 
NULL