Laravel 应用程序在升级到 php 8 后停止工作

Laravel app stopped working after upgrading to php 8

将我的 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

我尝试通过调查代码来解决问题,但没有成功

解决方案

正如 here 最新版本 laravel 6 所解释的那样,7 和 8 已对 php 8 进行了必要的更改。您所要做的就是:

1- 将 php 8 添加到您的 composer.json(我保留了 v7.4 以防生产服务器不支持 php 8)

"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. 但是您最好选择大多数库,因为它们有活跃的贡献者。

解释问题

如所述here

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.

我有类似的问题。但是在我注意到这个问题之前,我已经 运行 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
  1. 我运行命令:
brew link --overwrite composer
composer upgrade
composer update

这对我有用

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

php --version

那个版本是 PHP 8 吗?我对吗?这就是问题的原因:

PHP 8 在 PHP 类型系统中引入了多项改进,例如引入联合类型、混合类型等。

通过这些更改,Reflection API 的 ReflectionParameter 中的某些方法会产生不正确的结果。

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

  • 反射参数::getClass()
  • 反射参数::isArray()
  • 反射参数::isCallable()
  • 反射参数::getType()

将您的 php 版本降级到 7.4,您的 Laravel 应用程序运行得非常棒!