Skip to content

JsonSerializable throws warnings in newer PHP #180

@scottw-finao

Description

@scottw-finao

I'm using PHP 8.x and the lack of a return type :mixed on the various library JsonSerialize functions throws out warnings all over the place when using the lib in newer php.

There are a couple of fixes. For newer php, the preferred method is to add the :mixed return type on any definitions of JsonSerialize so they are compatible with the interface.

    // Json Serialize Code
    public function jsonSerialize(): mixed { /* code */ }

But you can also add a comment above the function that older version of php will ignore and newer ones will suppress the warning.

e.g.:

    // Json Serialize Code
    #[\ReturnTypeWillChange]
    public function jsonSerialize() { /* code */ }

See ReturnTypeWillChange

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions