Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReflectionParameter::getClass() method deprecated in php 8.0.1

Tags:

laravel

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

like image 656
Gopalkrish Avatar asked Nov 01 '25 18:11

Gopalkrish


2 Answers

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() .

like image 145
Yassir Abdelkarim Avatar answered Nov 03 '25 11:11

Yassir Abdelkarim


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.

like image 21
mrhn Avatar answered Nov 03 '25 09:11

mrhn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!