Skip to content

Report readonly property overriding non-abstract get-only virtual property - #7081

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
readonly-overriding-virtual-get
Nov 15, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
readonly-overriding-virtual-get

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

A readonly property overriding a get-only virtual property that isn't abstract was not reported:

class Base { public string $name { get => 'x'; } }
class Child extends Base { public readonly string $name = 'y'; }
// PHP: Cannot redeclare non-readonly property Base::$name as readonly Child::$name

php-src (do_inherit_property() in zend_inheritance.c) allows a readonly mismatch only when the parent property is abstract, which includes interface properties. The rule skipped the error whenever the prototype wasn't writable. The pattern becomes more tempting with readonly property defaults in PHP 8.6.

Verified against PHP 8.4: both new error cases (a regular parent class, and an abstract class with a non-abstract hooked property) are fatal errors, and the control case (abstract public string $prop { get; }) is valid.

…perty

PHP allows a readonly property to override a non-readonly one only when the
overridden property is abstract, not whenever it has no set operation.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1dfbd-11f8-71c3-bc25-13fc30a330ef
@ondrejmirtes
ondrejmirtes merged commit e0529cb into 2.3.x Nov 15, 2026
899 of 906 checks passed
@ondrejmirtes
ondrejmirtes deleted the readonly-overriding-virtual-get branch November 15, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant