2

I am getting this error:


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:




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 Here some part of code


 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;
        }
    }

Method ReflectionParameter::getClass() is deprecated .i think is getclass method is deprecated in version 8.0.1nstead of this i trying to using ReflectionParameter::getType()like link but not working .and also members suggest this Laravel app stopped working after upgrading to php 8 i tried this also but not working

Spencer Hill
  • 1,043
  • 2
  • 15
  • 38
Gopalkrish
  • 99
  • 1
  • 2
  • 7
  • this might help uoi https://stackoverflow.com/questions/65218631/laravel-app-stopped-working-after-upgrading-to-php-8 – John Lobo Jun 12 '21 at 14:15
  • i have tried this my composer gets updated still this error appeared Uncaught ErrorException: Method ReflectionParameter::getClass() is deprecated in C:\Users\User\Desktop\jk\vendor\laravel\framework\src\Illuminate\Container\Container.php:788 – Gopalkrish Jun 12 '21 at 14:25
  • 1
    it probably means you need to upgrade your Laravel version – apokryfos Jun 12 '21 at 15:06

4 Answers4

11

because ReflectionParameter::getClass() is deprecated in php 8 .

solution go to

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

and go to

protected function resolvePrimitive(ReflectionParameter $parameter)

and replace

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

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24
4

Laravel 5.4 seems to have incorrect platform requirements. Specifically that it requires PHP version >= 5.6 however it has code that will not work in PHP 8. Since 5.4 is end of life I would not expect any official code changes to make it work for PHP 8 so you would either need to fork and maintain your own Laravel 5.4 branch that solves these problems or upgrade your Laravel version to one that supports PHP 8.

The first Laravel version that supports PHP 8 is Laravel 6

apokryfos
  • 38,771
  • 9
  • 70
  • 114
3

You can replace it with getType(), which the documentation suggest. You have to create your own ReflectionClass, from the ReflectionType. This should do the trick.

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

I have this sandbox, that helped me assure it was working as intended.

mrhn
  • 17,961
  • 4
  • 27
  • 46
  • 1
    Just saw this question was about Laravel core code, i thought it was your own code and this answer would not apply, leaving it here as if it could help others – mrhn Jun 14 '21 at 07:35
-1

You might need to update your global composer.json with:

composer global update

I had some annoying issue so I hade to run:

composer global update --ignore-platform-reqs
Brainmaniac
  • 2,203
  • 4
  • 29
  • 53