You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter3在PHP8环境下get_instance引用赋值报错求解决方案

解决CodeIgniter3在PHP8下get_instance()引用导致的"Only variables should be assigned by reference"错误

核心修复方案

修改CodeIgniter3核心文件中get_instance()函数的声明,移除函数的引用标记,保持内部逻辑不变:

原代码(通常位于system/core/Common.php):

function &get_instance()
{
    return CI_Controller::get_instance();
}

修改为:

function get_instance()
{
    return CI_Controller::get_instance();
}

原理说明

  1. PHP8严格执行"仅变量可通过引用赋值"的规则,原函数的引用声明(&get_instance())会导致直接调用该函数(如链式调用、作为参数传递)时触发目标Notice错误。
  2. CodeIgniter3的CI_Controller::get_instance()本身返回单例模式下的CI超级对象实例,PHP中对象默认以引用方式传递,因此移除函数的引用声明不会破坏原有功能逻辑。

无需大规模改造代码的原因

项目中所有调用get_instance()的代码(例如$CI =& get_instance();或get_instance()->load->model('xxx');),在PHP8环境下即使去掉函数的引用声明,对象的引用传递特性依然生效,无需修改任何业务代码即可兼容。

修复后,错误处理器将恢复正常工作,CI超级对象也能被正确获取,不会再出现因该Notice导致的异常。

内容的提问来源于stack exchange,提问作者Kobus Myburgh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 19:32:35