Skip to content

Virtual property error message when combined with asymmetric visibility is confusing #19845

@DeveloperRob

Description

@DeveloperRob

Description

First of all, I am aware this is rather minor; however I went down completly the wrong route in trying to find the issue in my code, so I thought it was worth raising as an issue.

At the moment, you can't have a virtual property with only a get hook and asymmetric visibility. This makes sense, as there is no 'setting' possible. However the error message starts Read-only virtual property... which makes it sound like the issue is a get hook with a readonly property (which is also disallowed, but for entirely different reasons). This is made worse by the error message for readonly hooks also (correctly) referring to readonly.

Could the current wording used to refer to virtual properties with no set hook be changed to something other than read-only? I am not fantastic at naming, but I don't think ``Virtual property without set hook...` would be the worst message/wording.

As a final note, in relation to why this can occur - in my case, I had forgotten to add $this-> before referring to the property in question. Then, as I remembered readonly is the same as public protected(set) in terms of visibility (https://wiki.php.net/rfc/asymmetric-visibility-v2), I went off in completely the wrong direction when trying to find the issue.

The following code:

<?php

class Foo{
    public protected(set) ?string $bar{
        get => $bar;
    }
}

Resulted in this output:

Fatal error: Read-only virtual property Foo::$bar must not specify asymmetric visibility in /in/ngcDT on line 5

The following code:

<?php

class Foo{
    public readonly ?string $bar{
        get => $bar;
    }
}

Resulted in this output:

Fatal error: Hooked properties cannot be readonly in /in/l2QSE on line 4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions