ThinkPHP 8.0是基于PHP 8.0+的底层重构框架,要求PHP 8.1+运行,移除全局助手函数、动态查询魔术方法及think-helper包,强制使用命名空间调用(如\think\facade\Request::get),并严格遵循PHP 8.x语法与类型规范。

ThinkPHP 8.0 并非小版本迭代,而是对底层架构、安全模型和 PHP 兼容性的一次重构。很多在 TP6 或更早版本中“习以为常”的写法,在 TP8 中已彻底移除——不是警告,是直接报 Fatal error,页面白屏或接口中断。升级前必须逐项清理这些废弃点。
全局助手函数全部移除
input()、session()、cookie()、redirect()、url()、dump()、halt() 等函数不再作为全局函数存在,调用即报 Call to undefined function。
-
input('id') → 改用
\think\facade\Request::get('id')(推荐完整命名空间,避免 use 冲突) -
session('user') → 改用
\think\facade\Session::get('user') -
redirect('index/index') → 改为
return \think\facade\Redirect::to('index/index');注意:它必须作为返回值,否则跳转不生效 -
dump($data) → 改为
\think\facade\Debug::dump($data);halt($msg) 已无兼容层,改用throw new \think\Exception($msg)或\think\facade\Debug::halt($msg) - 删除
think-helper包及所有自定义别名配置(如 config/app.php 中的 alias)
模型动态查询方法停用
whereTime、whereLike、whereNull、whereBetween 等魔术方法被移除,调用会触发 Call to undefined method think\Model::whereTime()。
-
whereTime('create_time', 'today') → 拆解为
where('create_time', '>=', date('Y-m-d'))->where('create_time', ' -
whereLike('name', '%admin%') → 改为
where('name', 'like', '%admin%') -
whereIn('id', $ids) → 必须前置判空:
!empty($ids) ? whereIn('id', $ids) : whereRaw('1=0'),否则空数组会抛异常 - 兼容模式(
'strict' => false)仅作过渡,TP8.1+ 将完全移除,不可长期依赖
PHP 8.x 兼容性硬性要求
TP8 默认面向 PHP 8.0+ 运行,旧代码中残留的 PHP 废弃语法会立刻阻断执行。
立即学习“PHP免费学习笔记(深入)”;
-
each() 已物理删除 → 所有
while (list($k, $v) = each($arr))必须改为foreach ($arr as $k => $v) -
create_function() 已移除 → 替换为匿名函数,例如
array_map(function($v) { return $v * 2; }, $arr) - mysql_* 系列函数 不再存在 → 必须迁移到 PDO 或 ThinkPHP 自带的 Db 类
-
字符串偏移访问 $str[0] 在 PHP 8.0+ 触发 TypeError → 改用
substr($str, 0, 1)或$str{0}(后者已不推荐) - 确保所有方法签名匹配类型声明,子类重写父类方法时,返回类型、参数类型必须严格一致
第三方登录与 OAuth 集成变更
旧版 think-oauth 扩展无法适配 GitHub、GitLab 等平台的新安全策略,回调失败率极高。
- 弃用
think-oauth,改用league/oauth2-client+ 自定义 Provider - GitHub 登录必须启用 PKCE(传入
code_verifier和code_challenge),否则返回invalid_grant - state 参数必须由你生成并存入 session,不能依赖库自动处理,防止 TP8 的 session 生命周期与 PSR-7 请求错位
- 回调路由(如
/oauth/callback/github)建议关闭自动 session 初始化中间件,避免 header 已发送错误



















