【问题标题】:What causes the error "Can't use method return value in write context" in this Codeigniter 3 application?是什么导致此 Codeigniter 3 应用程序中的错误“无法在写入上下文中使用方法返回值”?
【发布时间】:2020-12-24 22:28:04
【问题描述】:

我正在使用 Codeigniter 3、Ion-Auth 和 Bootstrap 4 开发一个社交网络应用程序。您可以看到 Github repo HERE .

编辑用户的个人资料时,我会检查编辑表单中是否有新的用户照片(头像)。如果是,我使用它,如果不是,我使用(保留)users 表中已经存在的那个(文件路径为application/controllers/Auth.php):

$new_file = $this->upload->data('file_name');
$this->file_name = (isset($new_file) && !empty($new_file)) ? $new_file : $user->avatar;

上面的代码运行良好。

但是,我需要在会话中更新用户的照片,为此我在$this->file_name = (isset($new_file) && !empty($new_file)) ? $new_file : $user->avatar 下方添加了:

if (isset($new_file) && !empty($new_file)) {
    $this->session->userdata('user_avatar') = $new_file;
}

上述 if 语句导致错误“Can't use method return value in write context”。

我做错了什么?

【问题讨论】:

  • 可能是userdata()返回了一个值,可能需要找对应的方法写回数据。

标签: php codeigniter codeigniter-3


【解决方案1】:

有关 isset 和 empty 的使用,请参阅 Why check both isset() and !empty()。您不需要同时使用两者。

if (isset($new_file) && !empty($new_file)) {
    $this->session->userdata('user_avatar') = $new_file;
}

在这里您应该使用$this->session->set_userdata('user_avatar',$new_file)(根据用户指南)。

这样就变成了

if (!empty($new_file)) {
    $this->session->set_userdata('user_avatar',$new_file);
}

您甚至不需要考虑使用 isset(),因为您在上面的代码中定义了 $new_file,因此它将始终是“设置”,即 isset($new_file) 将始终为真。

但是在 $new_file 为空的情况下你打算做什么呢?这是你需要考虑的问题。

【讨论】:

    【解决方案2】:

    您通常会在调用执行写入的方法的 if 语句中遇到该错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多