Skip to content

Commit

Permalink
Updates from review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crell committed Oct 6, 2024
1 parent d09b3f4 commit 1f47661
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 65 deletions.
2 changes: 1 addition & 1 deletion language/oop5/final.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Bar extends Foo
</note>
<note>
<simpara>
A property that is declared <literal>private(set)</literal> is implicitly <literal>final</literal>.
A property that is declared <link linkend="language.oop5.visibility-members-aviz"><literal>private(set)</literal></link> is implicitly <literal>final</literal>.
</simpara>
</note>
</sect1>
Expand Down
3 changes: 2 additions & 1 deletion language/oop5/properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be ac
which prevents modification of the property after initialization. Prior to PHP 8.4.0
a <literal>readonly</literal> property is implicitly private-set, and may only be written to
from the same class. As of PHP 8.4.0, <literal>readonly</literal> properties are implicitly
<literal>protected(set)</literal>, so may be set from child classes. That may be overridden
<link linkend="language.oop5.visibility-members-aviz"><literal>protected(set)</literal></link>,
so may be set from child classes. That may be overridden
explicitly if desired.
<example>
<title>Example of readonly properties</title>
Expand Down
139 changes: 76 additions & 63 deletions language/oop5/visibility.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,20 @@ $obj2->printHello(); // Shows Public2, Protected2, Undefined
</programlisting>
</example>
</para>
<para>
As of PHP 8.4, properties may also have their
visibility set asymmetrically, with a different scope for
reading (<emphasis>get</emphasis>) and writing (<emphasis>set</emphasis>).
Specifically, the <emphasis>set</emphasis> visibility may be
specified separately, provided it is not more permissive than the
default visibility.
</para>
<para>
<example>
<title>Asymmetric Property visibility</title>
<programlisting role="php">
<sect3 xml:id="language.oop5.visibility-members-aviz">
<title>Asymmetric Property Visibility</title>
<para>
As of PHP 8.4, properties may also have their
visibility set asymmetrically, with a different scope for
reading (<emphasis>get</emphasis>) and writing (<emphasis>set</emphasis>).
Specifically, the <emphasis>set</emphasis> visibility may be
specified separately, provided it is not more permissive than the
default visibility.
</para>
<para>
<example>
<title>Asymmetric Property visibility</title>
<programlisting role="php">
<![CDATA[
<?php
class Book
Expand Down Expand Up @@ -121,54 +123,64 @@ $b->author = 'Pedro H. Peterson'; // Fatal Error
$b->pubYear = 2023; // Fatal Error
?>
]]>
</programlisting>
</example>
</para>
<para>There are a few caveats with asymmetric visibility to be aware of.</para>
<itemizedlist>
<listitem>
<simpara>
Only typed properties may have a separate <emphasis>set</emphasis> visibility.
</simpara>
<simpara>
The <emphasis>set</emphasis> visibility must be the same
as <emphasis>get</emphasis> or more restrictive. That is,
<code>public protected(set)</code> and <code>protected protected(set)</code>
are allowed, but <code>protected public(set) will cause a syntax error.</code>
</simpara>
<simpara>
If a property is <literal>public</literal> writeable, then the main visibility may be
omitted. That is, <code>public private(set)</code> and <code>private(set)</code>
will have the same result.
</simpara>
<simpara>
A property with <literal>private(set)</literal> visibility
is automatically <literal>final</literal>, and may not be redeclared in a child class.
</simpara>
<simpara>
Obtaining a reference to a property follows <emphasis>set</emphasis> visibility, not <emphasis>get</emphasis>.
That is because a reference may be used to modify the property value.
</simpara>
<simpara>
Similarly, trying to write to an array property involves both a <emphasis>get</emphasis> and
<emphasis>set</emphasis> operation internally, and therefore will follow the <emphasis>set</emphasis>
visibility, as that is always the more restrictive.
</simpara>
</listitem>
</itemizedlist>
<para>
When a class extends another, the child class may redefine
any property that is not <literal>final</literal>. When doing so,
it may widen either the main visibility or the <emphasis>set</emphasis>
visibility, provided that the new visibility is the same or wider
than the parent class. However, be aware that if a <literal>private</literal>
property is overridden, it does not actually change the parent's property
but creates a new property with a different internal name.
</para>
<para>
<example>
<title>Asymmetric Property inheritance</title>
<programlisting role="php">
</programlisting>
</example>
</para>
<itemizedlist>
<title>Caveats regarding asymmetric visibility</title>
<listitem>
<simpara>
Only typed properties may have a separate <emphasis>set</emphasis> visibility.
</simpara>
</listitem>
<listitem>
<simpara>
The <emphasis>set</emphasis> visibility must be the same
as <emphasis>get</emphasis> or more restrictive. That is,
<code>public protected(set)</code> and <code>protected protected(set)</code>
are allowed, but <code>protected public(set) will cause a syntax error.</code>
</simpara>
</listitem>
<listitem>
<simpara>
If a property is <literal>public</literal> writeable, then the main visibility may be
omitted. That is, <code>public private(set)</code> and <code>private(set)</code>
will have the same result.
</simpara>
</listitem>
<listitem>
<simpara>
A property with <literal>private(set)</literal> visibility
is automatically <literal>final</literal>, and may not be redeclared in a child class.
</simpara>
</listitem>
<listitem>
<simpara>
Obtaining a reference to a property follows <emphasis>set</emphasis> visibility, not <emphasis>get</emphasis>.
That is because a reference may be used to modify the property value.
</simpara>
</listitem>
<listitem>
<simpara>
Similarly, trying to write to an array property involves both a <emphasis>get</emphasis> and
<emphasis>set</emphasis> operation internally, and therefore will follow the <emphasis>set</emphasis>
visibility, as that is always the more restrictive.
</simpara>
</listitem>
</itemizedlist>
<para>
When a class extends another, the child class may redefine
any property that is not <literal>final</literal>. When doing so,
it may widen either the main visibility or the <emphasis>set</emphasis>
visibility, provided that the new visibility is the same or wider
than the parent class. However, be aware that if a <literal>private</literal>
property is overridden, it does not actually change the parent's property
but creates a new property with a different internal name.
</para>
<para>
<example>
<title>Asymmetric Property inheritance</title>
<programlisting role="php">
<![CDATA[
<?php
class Book
Expand All @@ -186,9 +198,10 @@ class SpecialBook extends Book
}
?>
]]>
</programlisting>
</example>
</para>
</programlisting>
</example>
</para>
</sect3>
</sect2>

<sect2 xml:id="language.oop5.visiblity-methods">
Expand Down

0 comments on commit 1f47661

Please sign in to comment.