Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.1 Readonly property cannot have a default value #5906

Closed
sofyco opened this issue Nov 2, 2021 · 5 comments
Closed

PHP 8.1 Readonly property cannot have a default value #5906

sofyco opened this issue Nov 2, 2021 · 5 comments

Comments

@sofyco
Copy link

sofyco commented Nov 2, 2021

Bug report

PHPStan with PHP 8.1 shows an error when you use the default value for a read-only property in the constructor.

https://wiki.php.net/rfc/readonly_properties_v2

As the property has no default value, the assignment in the constructor is initializing, and thus legal. The constructor property promotion feature was specifically designed for forward-compatibility with readonly properties.

Code snippet that reproduces the problem

final class ErrorResponse
{
    public function __construct(public readonly string $message, public readonly array $parameters = [])
    {
    }
}

has error:

Readonly property cannot have a default value.

Expected output

No errors should be detected.

@mergeable
Copy link

mergeable bot commented Nov 2, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@simPod
Copy link
Contributor

simPod commented Nov 16, 2021

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@7c8e81d

@sofyco
Copy link
Author

sofyco commented Nov 16, 2021

@ondrejmirtes Thank you!!!

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants