php - CodeIgniter 3 - 具有 PHP 空函数的 session 项 getter

标签 php codeigniter session conditional-statements is-empty

我刚刚注意到使用 CodeIgniter 3 session 系统时有些奇怪,我想知道这是问题还是 PHP 的正常现象。

为了解释,我已经初始化了这个变量:

$this->session->connected_client_id = 9;

当我这样做时:

$id = $this->session->connected_client_id
// Testing using "get_userdata" function
if (empty($this->session->get_userdata('connected_client_id'))) {
    echo "EMPTY $id";
} else {
    echo "NOT EMPTY $id";
}

没关系,我得到:NOT EMPTY 9

但是,当我这样做时:

$id = $this->session->connected_client_id
// Testing using the CodeIgniter 3 session getter
if (empty($this->session->connected_client_id))) {
    echo "EMPTY $id";
} else {
    echo "NOT EMPTY $id";
}

NOT 好吧,我得到:EMPTY 9

当我这样做的时候:

$id = $this->session->connected_client_id
// Testing using a variable as intermediate from the CI3 session getter
if (empty($id))) {
    echo "EMPTY $id";
} else {
    echo "NOT EMPTY $id";
}

没关系,我得到:NOT EMPTY 9

我正在使用一个变量作为前一个返回“EMPTY 9”的 getter 的中间值 ...
有人对此有解释吗?有没有一种正确的方法可以在没有这种“空洞的麻烦”的情况下实现这种检查?

最佳答案

它是......既是 - 在 PHP 中正常给定 CI_Session 类代码,但在 CodeIgniter 中也是一个问题,因为这个特定的用例应该工作但没有。

此补丁将在下一个 CodeIgniter 版本 (3.0.6) 中修复它:https://github.com/bcit-ci/CodeIgniter/commit/2c10f60586faf59b9380608c5a9bf01ff2522483

但是,我倾向于建议直接访问 $_SESSION 数组 - 这就是库为您提供的。

关于php - CodeIgniter 3 - 具有 PHP 空函数的 session 项 getter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36010546/

相关文章:

PHP:当回显 JavaScript 警报时,文本输入大小会减小

c# - 在服务器端保存值与在 ASP.net 中使用 Sessions 和 Request.QueryString

php - Recaptcha - 表单自定义

php - 使用 Codeigniter 进行复杂查询连接

php - pecl 在生产服务器上安装扩展问题

php - 如何在 CodeIgniter 中将我的 API 和随 secret 钥生成器从第三方 URL 获取到我的 Controller 中

ruby-on-rails - 在 Controller 中使用实例变量

PHP session 未在终端中正确显示

php - 如何将此 PHP RegEx 匹配模式转换为 .Net

PHP MySQL 查询 : match two tables with percentage match