php8.0使用elasticsearch8.8以后的版本报接口实现异常

1. 运行环境#

1). 当前使用的 Laravel 版本?#

9.52.16

2). 当前使用的 php 版本?#

PHP 版本:

8.0.28

3). 当前系统#

Windows 11

4). 业务环境#

开发环境

5). 相关软件版本#

Nginx,elasticsearch8.12.0

2. 问题描述?#

无法调用 es 包,比如

use Elastic\Elasticsearch\Response\Elasticsearch;
$es = new Elasticsearch();
dd($es);

报错如下:

In Elasticsearch.php line 87:

  Declaration of Elastic\Elasticsearch\Response\Elasticsearch::withStatus(int $code, string $reasonPhrase = ''): Psr\  
  Http\Message\ResponseInterface must be compatible with Psr\Http\Message\ResponseInterface::withStatus($code, $reaso  
  nPhrase = <default>)

Fatal error: Declaration of Elastic\Elasticsearch\Response\Elasticsearch::withStatus(int $code, string $reasonPhrase = ''): Psr\Http\Message\ResponseInterface must be compatible with Psr\Http\Message\ResponseInterface::withStatus($code, $reasonPhrase = <default>) in D:\wwwroot\xyh-demo.com\vendor\elasticsearch\elasticsearch\src\Response\Elasticsearch.php on line 87

找到位置是 MessageResponseTrait 的 87 行,代码如下:

laravel9使用elasticsearch8.12版本出现接口实现错误

接口定义:

laravel9使用elasticsearch8.12版本出现接口实现错误

经过调试:
只有在实现方法里面去掉 $int$code 的类型名才可以,在原生里面复原也没问题,找不到解决办法,只能将 es 退到了 8.0.0 版本是没问题的

此问题 php8.1 开始就是正常了,
使用 php8.0 的话,把包里面的接口通过自己定义的类实现也没问题。
不知道具体是什么原因引起的
有知道的朋友帮忙回复下,感谢

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 4
leo

看起来是其他包锁死了 psr/http-message 为 1.x 导致的,可以执行一下 composer why psr/http-message 看看是哪个包锁的

10个月前 评论
yanzhijia (楼主) 9个月前
leo

看起来是其他包锁死了 psr/http-message 为 1.x 导致的,可以执行一下 composer why psr/http-message 看看是哪个包锁的

10个月前 评论
yanzhijia (楼主) 9个月前

感觉是定义问题,接口类的方法没有定义返回值的类型,继承的方法也不允许有; 但是 php8.1 兼容了这种定义写法,8.1 之前不允许

10个月前 评论
yanzhijia (楼主) 9个月前