您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页Azalea\Session

Azalea\Session

来源:二三娱乐

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"

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务