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

PHP8.1升级后Google API客户端出现弃用警告,求解决方法

问题背景

我在MacOS 14系统上将PHP升级至8.1.25版本,同时把Composer升级到2.6.5(2023-10-06 10:11:52),执行composer update后完成了18个依赖包的安装,但运行项目时出现大量PHP弃用警告,而另一台运行Ubuntu22.04 LTS(PHP版本8.1.2-1ubuntu2.14)的系统上未出现此问题。

两类警告示例:

  1. 自定义代码参数顺序问题:
Deprecated: Optional parameter $parentId declared before required parameter $filesAfterDate is implicitly treated as a required parameter in /Volumes/Satechi.SSD.M2/www/htdocs/gdrive/catchupSongBook on line 277
  1. Google API客户端库返回类型兼容问题:
Deprecated: Return type of Google\Model::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Volumes/Satechi.SSD.M2/www/cgi-bin/GoogleRelated/vendor/google/apiclient/src/Model.php on line 256
解决步骤

一、修复自定义代码的参数警告

打开/Volumes/Satechi.SSD.M2/www/htdocs/gdrive/catchupSongBook文件第277行,调整函数参数顺序:

  • 将必填参数$filesAfterDate移到可选参数$parentId之前
  • 示例:把function myFunc($parentId = null, $filesAfterDate)改为function myFunc($filesAfterDate, $parentId = null)

PHP 8.0+严格要求可选参数不能置于必填参数之前,旧版本PHP会隐式兼容,但新版本会抛出弃用警告。

二、处理Google API客户端库的返回类型警告

方案1:升级Google API客户端库(推荐)

当前安装的google/apiclient为v2.15.1,该版本对PHP 8.1兼容性不足。执行以下命令升级到兼容版本:

composer require google/apiclient:^2.16

新版本库已修复返回类型与PHP 8.1接口不兼容的问题,会自动添加#[ReturnTypeWillChange]属性或更新返回类型声明。

方案2:临时屏蔽弃用警告(仅临时应急)

若暂时无法升级库,可通过以下方式屏蔽警告:

  1. 修改php.ini配置:找到error_reporting项,移除E_DEPRECATED:
    error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
    
    执行php --ini找到当前生效的php.ini路径,修改后重启Web服务或CLI进程。
  2. 在项目入口代码开头添加:
    error_reporting(error_reporting() & ~E_DEPRECATED);
    

三、关于Ubuntu系统无警告的原因

Ubuntu的PHP 8.1.2版本默认错误报告级别未开启E_DEPRECATED,或该版本对部分弃用规则执行更宽松;而MacOS的PHP 8.1.25版本默认启用了更严格的错误报告,因此会显示这些警告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 01:49:51