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"