Skip to content

Covariant properties not properly supported (PHP 8.4) #12466

@petercv

Description

@petercv

Bug report

As of PHP 8.4 it is possible to have covariant properties. From the documentation:

As of PHP 8.4.0, with the addition of abstract properties (on an interface or abstract class) and virtual properties, it is possible to declare a property that has only a get or set operation. As a result, abstract properties or virtual properties that have only a "get" operation required may be covariant. Similarly, an abstract property or virtual property that has only a "set" operation required may be contravariant.

PHPStan does understand this when using interfaces, but when using abstract classes it incorrectly reports 2 errors:

  • Class ... has an uninitialized property $.... Give it default value or assign it in the constructor.
  • Type ... of property ...::... is not the same as type ... of overridden property ...::....

Code snippet that reproduces the problem

https://phpstan.org/r/3ee7f161-1826-4eba-a8a9-63b810b41ae4

Expected output

No errors.

Did PHPStan help you today? Did it make you happy in any way?

Very happy that PHPStan already supports PHP 8.4 at all. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions