Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions language/oop5/properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,35 @@ $test1->prop = "foobar";
</programlisting>
</example>
</para>
<note>
<para>
Readonly properties can only be initialized by a direct assignment.
An uninitialized readonly property cannot be initialized by reference, not even
from the scope where it has been declared: passing it to a function parameter that
expects to be passed by reference, or assigning it by reference, results in an
<exceptionname>Error</exceptionname> exception.
<informalexample>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php

class Test {
public readonly array $matches;

public function __construct(string $subject) {
// Illegal initialization by reference.
preg_match('/\d+/', $subject, $this->matches);
// Error: Cannot indirectly modify readonly property Test::$matches
}
}

new Test('abc 42');
?>
]]>
</programlisting>
</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.
Expand Down