Laravel应用在升级到PHP 8后停止工作

37

更新我的 Mac 到 PHP 8 后,Laravel 应用停止工作,这是我收到的错误信息:

Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on line 871

Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on line 945

Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on line 871

Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on line 945

我已经尝试通过调查代码来解决这个问题,但是没有成功。

4个回答

43

解决方案

此处所述,最新版本的laravel 6、7和8已经进行了适配php8的更改,您只需执行以下操作:

1- 将php 8添加到您的composer.json文件中(为了防止生产服务器尚不支持php 8,我保留了v7.4)。

"php": "^7.4|^8.0",

2- 运行 composer update 命令来将你的 Laravel 更新到最新版本。

composer update

3- 确保更新以下库文件,因为它们存在于所有的Laravel应用程序中。

PHP to php:^8.0
Faker to fakerphp/faker:^1.9.1
PHPUnit to phpunit/phpunit:^9.3

4- 检查是否有其他需要更新的库,如果它们还没有支持php 8,则进行贡献。但是你应该可以通过大多数库,因为它们有活跃的贡献者。

解释问题

此处所述

PHP 8 introduces several improvements in PHP type systems such as the introduction of Union Types, mixed type, and a few more.

With these changes, certain methods in Reflection API's ReflectionParameter yield incorrect results.

In PHP 8, the following methods from ReflectionParameter class is deprecated:

ReflectionParameter::getClass()
ReflectionParameter::isArray()
ReflectionParameter::isCallable()

ReflectionParamter::getType() is the recommended way to replace the deprecated methods. This method is available in PHP 7.0 and later.


我更新了我的composer.json文件,但是当我运行composer update命令时,我仍然收到“已弃用”的错误,似乎是一个进退两难的情况,因为composer在启动时运行“artisan clear-compiled”命令,这已经导致了错误!有什么建议吗? 我也不太确定你的第3步意味着什么,我是否需要进入libraries并检查它们的composer.json文件? - matthiku
我通过暂时从composer json中删除“pre-update-cmd”来使其工作。 我还不得不升级laravel/socialite到“^5.1”。 无论如何,感谢您之前的帮助! - matthiku
@matthiku 我认为你所做的事情并不好(删除 pre-update-cmd),我的第三步是在你的 composer.yml 文件中更新这些依赖项。 - Pezhvak
运行composer update后,我收到一个错误信息Killed。为了使其正常工作,我必须升级到版本2。 - adambg
Faker 1.9.1 在我的情况下会抛出错误。相反,我使用了 "fzaninotto/faker": "~1.4",它可以正常工作。 - Mohamed Shahid
1
更新composer.json后出现问题1:
  • laravel/framework[v5.7.0,...,5.7.x-dev]需要php ^7.1.3 -> 您的php版本(8.1.2)不满足该要求。
- Palo

2

检查您虚拟机(xampp或服务器)中的php版本。

php --version

这是PHP 8的版本吗?我是正确的吗?这就是问题的原因:

PHP 8在PHP类型系统方面引入了几项改进,如引入联合类型、混合类型等。

由于这些变化,在Reflection API的ReflectionParameter中的某些方法会产生错误的结果。

在PHP 8中,ReflectionParameter类的以下方法已被弃用:

  • ReflectionParameter::getClass()
  • ReflectionParameter::isArray()
  • ReflectionParameter::isCallable()
  • ReflectionParamter::getType()

将您的PHP版本降级到7.4,您的Laravel应用程序将像魅力一样运作!


0

我遇到了类似的问题。但在发现这个问题之前,我已经运行了brew updatebrew cleanup。我的解决方案如下:

  1. 我从brew cleanup中注意到了这个错误:
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/node
Target /usr/local/bin/node
already exists. You may want to remove it:
  rm '/usr/local/bin/node'

To force the link and overwrite all conflicting files:
  brew link --overwrite heroku-node

我运行了以下命令:
brew link --overwrite composer
composer upgrade
composer update

那是我成功的方法


0
如果你正在使用 Valet,你应该执行以下操作:
  1. 将 PHP 版本从 8+ 降级到 7.4:valet isolate php@7.4
  2. 然后使用 Valet 运行 composer update:valet composer update

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接