[doc-en] master: PHP 8.6: Document readonly property defaults (#6102)

From: Date: Wed, 18 Nov 2026 14:43:16 +0000
Subject: [doc-en] master: PHP 8.6: Document readonly property defaults (#6102)
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-23977@lists.php.net to get a copy of this message
Author: Jordi Kroon (jordikroon) Committer: GitHub (web-flow) Pusher: jordikroon Date: 2026-11-18T15:43:12+01:00 Commit: https://github.com/php/doc-en/commit/e4676cd5278b301e0f83938a4d5798ab29198868 Raw diff: https://github.com/php/doc-en/commit/e4676cd5278b301e0f83938a4d5798ab29198868.diff PHP 8.6: Document readonly property defaults (#6102) Changed paths: M appendices/migration86/new-features.xml M language/oop5/basic.xml M language/oop5/properties.xml Diff: diff --git a/appendices/migration86/new-features.xml b/appendices/migration86/new-features.xml index 6152d3f2cd7e..243e3c99ea73 100644 --- a/appendices/migration86/new-features.xml +++ b/appendices/migration86/new-features.xml @@ -10,7 +10,11 @@ <simpara> <link linkend="language.oop5.properties.readonly-properties">Readonly - properties</link> may now declare default values. + properties</link>, including those of + <link linkend="language.oop5.basic.class.readonly">readonly classes</link>, + may now declare default values. + The default value counts as the initialization of the property, so the + property cannot be assigned again, not even from the constructor. <!-- RFC: https://wiki.php.net/rfc/readonly_property_defaults --> </simpara> diff --git a/language/oop5/basic.xml b/language/oop5/basic.xml index 5ff70c006efa..89a8aaadb56c 100644 --- a/language/oop5/basic.xml +++ b/language/oop5/basic.xml @@ -186,6 +186,11 @@ readonly class Foo ]]> </programlisting> </informalexample> + <simpara> + As of PHP 8.6.0, the properties of a <modifier>readonly</modifier> + class may declare + <link linkend="language.oop5.properties.readonly-properties">default values</link>. + </simpara> <para> A <modifier>readonly</modifier> class can be <link linkend="language.oop5.basic.extends">extended</link> diff --git a/language/oop5/properties.xml b/language/oop5/properties.xml index d7e11e6b0f7f..33018f0802c5 100644 --- a/language/oop5/properties.xml +++ b/language/oop5/properties.xml @@ -286,28 +286,47 @@ new Test('abc 42'); </informalexample> </para> </note> - <note> - <para> - Specifying an explicit default value on readonly properties is not allowed, because a readonly property with a default value is essentially the same as a constant, and thus not particularly useful. - <informalexample> - <programlisting role="php" annotations="non-interactive"> + <para> + As of PHP 8.6.0, a readonly property may declare a default value. + The default value counts as the initialization of the property, + so the property cannot be assigned again, not even from the constructor. + This makes it possible to implement a get-only + <link linkend="language.oop5.interfaces.properties">interface property</link> + with a fixed value, without having to assign it in a constructor. + Prior to PHP 8.6.0, declaring a default value on a readonly property + resulted in a compile-time error. + <informalexample> + <programlisting role="php" annotations="non-interactive"> <![CDATA[ <?php class Test { - // Fatal error: Readonly property Test::$prop cannot have default value + // The default value initializes the property. public readonly int $prop = 42; } -?> + +$test = new Test(); +var_dump($test->prop); // int(42) + +// The property has already been initialized by its default value. +$test->prop = 43; +// Error: Cannot modify readonly property Test::$prop ]]> - </programlisting> - </informalexample> - </para> + </programlisting> + </informalexample> + </para> + <note> + <simpara> + A default value on a + <link linkend="language.oop5.decon.constructor.promotion">promoted constructor parameter</link> + is the default value of the parameter, not of the property. + </simpara> </note> <note> - <para> + <simpara> Readonly properties cannot be <function>unset</function> once they are initialized. However, it is possible to unset a readonly property prior to initialization, from the scope where the property has been declared. - </para> + A readonly property with a default value is initialized when the object is created, and can therefore never be unset. + </simpara> </note> <para> Modifications are not necessarily plain assignments, all of the following will also result in an <classname>Error</classname> exception:

« previous php.doc.cvs (#23977) next »