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行,代码如下:
接口定义:
经过调试:
只有在实现方法里面去掉$int
和$code
的类型名才可以,在原生里面复原也没问题,找不到解决办法,只能将es退到了8.0.0版本是没问题的
此问题php8.1开始就是正常了,
使用php8.0的话,把包里面的接口通过自己定义的类实现也没问题。
不知道具体是什么原因引起的
有知道的朋友帮忙回复下,感谢
高认可度评论:
看起来是其他包锁死了
psr/http-message
为 1.x 导致的,可以执行一下composer why psr/http-message
看看是哪个包锁的看起来是其他包锁死了
psr/http-message
为 1.x 导致的,可以执行一下composer why psr/http-message
看看是哪个包锁的感觉是定义问题,接口类的方法没有定义返回值的类型,继承的方法也不允许有; 但是 php8.1 兼容了这种定义写法,8.1 之前不允许