Illuminate\Container\Container::get($id) 的声明必须与 Psr\Container\ContainerInterface::get(string $id) 兼容

Declaration of Illuminate\Container\Container::get($id) must be compatible with Psr\Container\ContainerInterface::get(string $id)

安装了新的 Laravel 8 项目并在加载第一个实例时出现以下错误。这很奇怪,因为我把它放在一边,后来从 Laravel 5.8 -> 6 升级了另一个项目(工作正常),当我去检查站点时遇到了类似的错误。

我已经清除了 composer 缓存,删除了 vendor 文件夹并重新安装,但似乎无法弄清楚。过去一天我一直在为此绞尽脑汁,似乎某些软件包版本不正确,但我不确定从哪里开始进行故障排除。

( !) 致命错误:Illuminate\Container\Container::get($id) 的声明必须与 /home/vagrant/ps/vendor/laravel/framework/src/Illuminate/Container/Container 中的 Psr\Container\ContainerInterface::get(string $id) 兼容。php 第 15 行

Call Stack
#   Time    Memory  Function    Location
1   0.0001  357784  {main}( )   .../index.php:0
2   0.0133  502360  require_once( '/home/vagrant/ps/bootstrap/app.php' )    .../index.php:47
3   0.0133  502360  spl_autoload_call ( )   .../app.php:14
4   0.0133  502424  Composer\Autoload\ClassLoader->loadClass( ) .../app.php:14
5   0.0133  502424  Composer\Autoload\includeFile( )    .../ClassLoader.php:322
6   0.0135  578336  include( '/home/vagrant/ps/vendor/laravel/framework/src/Illuminate/Foundation/Application.php' )    .../ClassLoader.php:444
7   0.0135  578336  spl_autoload_call ( )   .../Application.php:29
8   0.0135  578392  Composer\Autoload\ClassLoader->loadClass( ) .../Application.php:29
9   0.0135  578392  Composer\Autoload\includeFile( )    .../ClassLoader.php:322
10  0.0138  588440  include( '/home/vagrant/ps/vendor/laravel/framework/src/Illuminate/Container/Container.php' )   .../ClassLoader.php:444

您使用的 PHP 是什么版本?看起来这个问题的发生是因为五天前发布的 v1.0.0 和 v.1.1.0 之间的静态类型添加到 psr/container 以弃用 PHP < 7.2

https://github.com/php-fig/container/blob/1.0.0/src/ContainerInterface.php#L23

https://github.com/php-fig/container/blob/1.1.0/src/ContainerInterface.php#L22

固定 psr/container 到 v1.0.0 应该可以解决这个问题,直到更新 Illuminate

( ! ) Fatal error: Declaration of Illuminate\Container\Container::get($id) must be compatible with Psr\Container\ContainerInterface::get(string $id) in /home/vagrant/ps/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 15

在我的例子中,我使用的是 composer 1.10 并升级到 2.0 版。 作曲家将降级到 Psr\Container 的 1.0.0 版本,如果您需要返回作曲家 1,请使用命令:

composer self-update --1