Session 会话类
不建议直接使用
$_SESSION
超全局变量
⚠️ Session 构造函数已私有,无法通过 new 方式实例化,仅通过 控制器 的
getSession
方法获得
// in controller-action
$session = $this->getSession();
$foo = $session->get('foo', 'bar');
$session->set('foo', 'new value');
Session::get
获取会话变量
mixed Session::get ( string $key [, mixed $default = null] )
参数
$key - 会话变量键名
$default - 当键名不存在于$_SESSION
时,返回的默认值,默认为NULL
返回值
变量值范例
// 当 $_SESSION['foo'] 存在则返回值,否则返回 NULL
$session->get('foo');
// 当 $_SESSION['foo'] 存在则返回值,否则返回字符串 "bar"
$session->get('foo', 'bar');
Session::set
设置会话变量
void Session::set ( string $key, mixed $value )
参数
$key - 会话变量键名
$value - 变量值返回值
无范例
$session->set('foo', [1, 2, 3]);
$session->get('foo'); // 返回数组 [1, 2, 3]
Session::clean
清除所有会话变量
void Session::clean ( void )
参数
无返回值
无范例
$session->clean();
$session->get('foo', 'bar'); // 由于 $_SESSION 被清空,总是返回字符串 "bar"