简体   繁体   中英

Slim PHP framework gives Return type error

I have been using Slim PHP framework to provide my API service ( https://api.rsywx.com ).

Recently, I updated my PHP to 8.1 and when I visited the site, it gives the following error:

{
    "statusCode": 500,
    "error": {
        "type": "SERVER_ERROR",
        "description": "ERROR: Return type of App\\Application\\Actions\\ActionPayload::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice on line 67 in file \/home\/tr\/www\/api.rsywx.com\/src\/Application\/Actions\/ActionPayload.php."
    }
}

I have not changed anything in my source code, so I guess the problem raises from Slim framework itself.

Any hints to solve this issue?

As of PHP version 8.1.0, there was an update to the Serializable interface.

You could either use the #[\ReturnTypeWillChange attribute temporarily suppress the notice on the specified line then test further and address other occurrences in a similar fashion.

On the other hand, add the mixed type hint to your function as below, then test further in same fashion to fix subsequent occurrences.

public function jsonSerialize(): mixed
{
  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM