我刚刚注意到使用 CodeIgniter 3 会话系统有些奇怪,我想知道这是 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";
}
没关系,我得到:不空 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";
}
这不行,我得到:空 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";
}
没关系,我得到:不空 9
我正在使用一个变量作为返回"EMPTY 9"的上一个 getter 的中间变量......
有人对此有解释吗?有没有一种正确的方法可以在没有这种"空洞的麻烦"的情况下实现此检查?
这是...两者都 - 在 PHP 中正常,给定CI_Session
类代码,但在 CodeIgniter 中也是一个问题,因为这个特定的用例应该有效但不能。
此修补程序将在下一个 CodeIgniter 版本 (3.0.6) 中修复它:https://github.com/bcit-ci/CodeIgniter/commit/2c10f60586faf59b9380608c5a9bf01ff2522483
但是,我倾向于建议直接访问$_SESSION
数组 - 无论如何,这就是库为您提供的。