【问题标题】:ReflectionParameter::getClass() method deprecated in php 8.0.1php 8.0.1 中不推荐使用 ReflectionParameter::getClass() 方法
【发布时间】:2021-08-29 03:28:40
【问题描述】:

我收到此错误:


PS C:\Users\User\Desktop\jk> php artisan serve
PHP Fatal error:  Uncaught ErrorException: Method ReflectionParameter::getClass() is deprecated in C:\Users\User\Desktop\jk\vendor\laravel\framework\src\Illuminate\Container\Container.php:788
Stack trace:




作曲家.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.1 中已弃用,而不是我尝试使用 ReflectionParameter::getType()like link 但不起作用。而且成员也建议这样做 Laravel app stopped working after upgrading to php 8我也试过了,但没用

【问题讨论】:

  • 我已经试过这个我的作曲家得到更新仍然出现这个错误 Uncaught ErrorException: Method ReflectionParameter::getClass() is deprecated in C:\Users\User\Desktop\jk\vendor\laravel\framework\ src\Illuminate\Container\Container.php:788
  • 这可能意味着你需要升级你的 Laravel 版本

标签: laravel


【解决方案1】:

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

解决办法

vendor\laravel\framework\src\Illuminate\Container\Container.php

然后去

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

替换

$parameter->getClass()** with **$parameter->getType()->getName().

【讨论】:

  • 这不是@mrhn 接受的答案吗?
【解决方案2】:

Laravel 5.4 似乎有不正确的平台要求。具体来说,它需要 PHP 版本 >= 5.6,但是它的代码在 PHP 8 中不起作用。由于 5.4 已经结束,我不希望任何官方代码更改使其适用于 PHP 8,因此您要么需要 fork 和维护您自己的 Laravel 5.4 分支来解决这些问题,或者将您的 Laravel 版本升级到支持 PHP 8 的版本。

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

【讨论】:

  • 感谢您的回答,虽然我正在运行 Laravel 5.8 - 这适用于那个吗?
  • @SpencerHill Laravel 5.8 的版本要求为 PHP ^7.1.3,因此不明确支持 PHP 8
【解决方案3】:

您可以将其替换为getType(),这是documentation 建议的。您必须从ReflectionType 创建自己的ReflectionClass。这应该可以解决问题。

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

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

【讨论】:

  • 刚看到这个问题是关于 Laravel 核心代码的,我以为是你自己的代码,这个答案不适用,把它留在这里,好像它可以帮助别人一样
【解决方案4】:

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

composer global update

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

composer global update --ignore-platform-reqs

【讨论】:

    猜你喜欢
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2014-05-05
    • 2012-02-22
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多