求助:PHP出现「尝试访问null类型值的数组偏移」错误
错误提示
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\dev1\freelance\app\core\Controller.php 第245行
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\dev1\freelance\app\core\Controller.php 第246行
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\dev1\freelance\app\core\Controller.php 第250行
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\dev1\freelance\app\core\Controller.php 第251行
相关代码
/** * This is the method from where you can pass data to the Header view * * @return string */ private function getHeader() { $data = [ 'languages_list' => $this->languages, 'language' => $this->language ]; $data['url'] = $this->url[0]; /*Use User Library*/ $user = $this->library('User'); $data['user'] = $user->data(); $data['user_isloggedin'] = $user->isLoggedIn(); /*Use Client Model*/ $clientModel = $this->model('Client'); $data['client_unread_messages'] = $clientModel->unread_messages($data['user']['userid']); $data['client_unread_notifications'] = $clientModel->unread_notifications($data['user']['userid']); /*Use Freelancer Model*/ $freelancerModel = $this->model('Freelancer'); $data['freelancer_unread_messages'] = $freelancerModel->unread_messages($data['user']['userid']); $data['freelancer_unread_notifications'] = $freelancerModel->unread_notifications($data['user']['userid']); return $this->view->render($data, 'shared/header'); }
问题原因
警告核心是**$data['user']的值为null**,当代码尝试访问$data['user']['userid']时,触发"尝试访问null类型值的数组偏移"警告。通常出现在用户未登录时,$user->data()方法返回null而非用户信息数组。
解决方法
在调用需要userid的模型方法前,先判断用户登录状态,仅登录时执行相关逻辑:
private function getHeader() { $data = [ 'languages_list' => $this->languages, 'language' => $this->language ]; $data['url'] = $this->url[0]; /*Use User Library*/ $user = $this->library('User'); $data['user'] = $user->data(); $data['user_isloggedin'] = $user->isLoggedIn(); // 仅用户已登录且存在用户数据时,获取未读消息和通知 if ($data['user_isloggedin'] && !empty($data['user'])) { /*Use Client Model*/ $clientModel = $this->model('Client'); $data['client_unread_messages'] = $clientModel->unread_messages($data['user']['userid']); $data['client_unread_notifications'] = $clientModel->unread_notifications($data['user']['userid']); /*Use Freelancer Model*/ $freelancerModel = $this->model('Freelancer'); $data['freelancer_unread_messages'] = $freelancerModel->unread_messages($data['user']['userid']); $data['freelancer_unread_notifications'] = $freelancerModel->unread_notifications($data['user']['userid']); } else { // 未登录时初始化变量,避免视图出现未定义变量警告 $data['client_unread_messages'] = 0; $data['client_unread_notifications'] = 0; $data['freelancer_unread_messages'] = 0; $data['freelancer_unread_notifications'] = 0; } return $this->view->render($data, 'shared/header'); }
额外优化建议
- 修改
User::data()方法,用户未登录时返回空数组而非null,这样即使访问$data['user']['userid']只会得到null,不会触发数组偏移警告。 - 使用PHP7+的null合并运算符简化取值:
$userId = $data['user']['userid'] ?? 0;,避免直接访问可能不存在的数组键。
内容的提问来源于stack exchange,提问作者Christo Lochenberg

