ReflectionParameter::getClass()方法在php 8.0.1中已弃用



我收到这个错误:


PS C:UsersUserDesktopjk> php artisan serve
PHP Fatal error:  Uncaught ErrorException: Method ReflectionParameter::getClass() is deprecated in C:UsersUserDesktopjkvendorlaravelframeworksrcIlluminateContainerContainer.php:788
Stack trace:


Composer.json

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": "^7.4|^8.0",
"laravel/framework": "5.4.*",
"laravelcollective/html": "^5.3.0",
"guzzlehttp/guzzle": "^6.3",
"doctrine/dbal": "^2.9"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "^9.3",
"symfony/css-selector": "3.1.*",
"symfony/dom-crawler": "3.1.*"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-root-package-install": [
"php -r "file_exists('.env') || copy('.env.example', '.env');""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\Foundation\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\Foundation\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist",
"platform": {
"php": "8.0.1"
}
}
}

Container.php这里是代码的一部分


protected function resolveClass(ReflectionParameter $parameter)
{
try {
return $this->make($parameter->getClass()->name);
}
// If we can not resolve the class instance, we will check to see if the value
// is optional, and if it is we will return the optional parameter value as
// the value of the dependency, similarly to how we do this with scalars.
catch (BindingResolutionException $e) {
if ($parameter->isOptional()) {
return $parameter->getDefaultValue();
}
throw $e;
}
}

方法ReflectionParameter::getClass((被弃用。我认为getClass方法在版本8.0中被弃用。首先,我尝试使用类似ReflectionParameters::getType((的链接,但不起作用。成员们也建议这样做Laravel应用程序在升级到php8后停止工作,我也尝试过,但不工作

因为ReflectionParameter::getClass((在php 8中已弃用。

解决方案进入

vendorlaravelframeworksrcIlluminateContainerContainer.php

然后转到

受保护的函数resolvePrimitive(ReflectionParameter$parameter(

并替换

CCD_ 1。

Laravel 5.4似乎有不正确的平台要求。特别是它需要PHP版本>=5.6,但是它有一些代码在PHP8中不起作用。由于5.4已经过时了,我不希望任何官方代码的更改能使其适用于PHP 8,所以你要么需要分叉并维护你自己的Laravel 5.4分支来解决这些问题,要么将你的Laraver版本升级到支持PHP 8的版本。

第一个支持PHP 8的Laravel版本是Laravel 6

您可以将其替换为文档中建议的getType()。您必须从ReflectionType创建自己的ReflectionClass。这应该奏效。

$reflectionClass = new ReflectionClass($reflectionParam->getType()->getName());

我有这个沙盒,它帮助我确保它按预期工作。

您可能需要用更新您的全局composer.json

composer global update

我遇到了一些烦人的问题,所以我不得不运行:

composer global update --ignore-platform-reqs

最新更新