PHP会话,为什么数组设置为1


PHP Session, why array is set to 1

我正在创建一个使用会话存储消息的通知类。我需要将它们创建为一个多维数组,这样我就可以利用不同的"命名空间",从而防止消息显示在错误的页面上。

这里有一个例子:

print_r($_SESSION)
Array
(
    [EVENT_CMS] => Array
    (
        [Notifier] => Array
        (
            [0] => 'Your settings have been saved.'
            [1] => 'You must re-upload...'
        )
    )
)

现在在设置页面上,这些消息将通过调用正确的方法打印出来。

我在类中设置消息容器时遇到问题。这就是我的构造函数的样子:

public function __construct($namespace = 'Notifier') {
    $this->_session_start();
    if(defined('SESSION_NAMESPACE')){
        $this->notifications =& $_SESSION[SESSION_NAMESPACE][$namespace];
    } else {
        $this->notifications =& $_SESSION[$namespace];
    }
}

(SESSION_NAMEMESPACE常量已定义,因此执行真正的块。)

$Notify = new Notifier();
$Notify->add($_GET['test']);
print_r($_SESSION);

上面的代码给了我这个数组:

$_SESSION
Array
(
    [EVENT_CMS] => Array
    (
        [Notifier] => 1
    )
)

add message方法应该更新会话,对吧?既然通知数组是引用?对update_session()的调用对输出没有影响。。。

public function add($message, $class = NULL) {
    $message_node = $message;
    $this->notifications[] = $message_node;
    $this->update_session();
}
public function update_session(){
    $this->SESSION[$this->namespace] &= $this->notifications;
}

您将按位运算符与引用运算符混合在一起。update_session()方法中使用了错误的方法。