Skip to content

Commit

Permalink
Tweaks and nitpicks from Girgas
Browse files Browse the repository at this point in the history
Co-authored-by: Gina Peter Banyard <girgias@php.net>
  • Loading branch information
Crell and Girgias authored Nov 20, 2024
1 parent 7c0d3ca commit 9cc604e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
10 changes: 5 additions & 5 deletions language/oop5/interfaces.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@
<sect2 xml:id="language.oop5.interfaces.properties">
<title>Properties</title>
<para>
As of PHP 8.4, interfaces may also declare properties. If they do, the declaration must specify if the
As of PHP 8.4.0, interfaces may also declare properties. If they do, the declaration must specify if the
property is to be readable, writeable, or both. The interface declaration applies only to public read
and write access.
</para>
<para>
An class may satisfy an interface property in multiple ways. It may define a public property. It may
define a public <link linkend="language.oop5.property-hooks.virtual">virtual property</link> that implements
only the corresponding hook. Or a read property may be satisfied by a <literal>readonly</literal> property. An interface property that is settable may not
be <literal>readonly</literal>, however.
only the corresponding hook. Or a read property may be satisfied by a <literal>readonly</literal> property.
However, an interface property that is settable may not be <literal>readonly</literal>.
</para>
<example>
<title>Interface properties example</title>
Expand Down Expand Up @@ -141,8 +141,8 @@ class C2 implements I
private string $all = '';
// Uses only a get hook to create a virtual property.
// This satisfies the "public get" requirement. It is not
// writeable, but that is not required by the interface.
// This satisfies the "public get" requirement.
// It is not writeable, but that is not required by the interface.
public string $readable { get => strtoupper($this->writeable); }
// The interface only requires the property be settable,
Expand Down
9 changes: 5 additions & 4 deletions language/oop5/property-hooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ print $example->foo;
</programlisting>
</example>
<para>
The <varname>$foo</varname> property ends in <literal>{}</literal>, rather than a semicolon.
The <varname>foo</varname> property ends in <literal>{}</literal>, rather than a semicolon.
That indicates the presence of hooks. Both a <literal>get</literal> and <literal>set</literal>
hook are defined, although it is allowed to define only one or the other. Both hooks have a body,
denoted by <literal>{}</literal>, that may contain arbitrary code.
Expand All @@ -81,7 +81,7 @@ print $example->foo;
using the same syntax as a method. The type must be either the same as the type of the property,
or <link linkend="language.oop5.variance.contravariance">contravariant</link> (wider) to it.
For instance, a property of type <type>string</type> could
have a <literal>set</literal> hook that accepts <type>string|Stringable</type>,
have a <literal>set</literal> hook that accepts <type class="union"><type>string</type><type>Stringable</type></type>,
but not one that only accepts <type>array</type>.
</para>
<para>
Expand Down Expand Up @@ -254,7 +254,8 @@ $s->area = 30; // Error, as there is no set operation defined.
</para>
<para>
The most notable implication of this is that non-trivial hooks may sub-call to an
arbitrarily complex method if they wish.</para>
arbitrarily complex method if they wish.
</para>
<example>
<title>Calling a method from a hook</title>
<programlisting role="php">
Expand Down Expand Up @@ -319,7 +320,7 @@ class Person {
<example>
<title>Final hooks</title>
<programlisting role="php">
<![CDATA[
<![CDATA[
<?php
class User
{
Expand Down
3 changes: 2 additions & 1 deletion language/oop5/variance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ Fatal error: Uncaught TypeError: Argument 1 passed to Animal::eat() must be an i
and "set" operations must be contravariant. The only way for a property to satisfy both requirements is to be invariant.
</para>
<para>
Since PHP 8.4, with the addition of abstract properties (on an interface or abstract class) and <link linkend="language.oop5.property-hooks.virtual">virtual properties</link>,
As of PHP 8.4.0, with the addition of abstract properties (on an interface or abstract class) and
<link linkend="language.oop5.property-hooks.virtual">virtual properties</link>,
it is possible to declare a property that has only a get or set operation.
As a result, abstract properties or virtual properties that have only a "get" operation required may be covariant.
Similarly, an abstract property or virtual property that has only a "set" operation required may be contravariant.
Expand Down

0 comments on commit 9cc604e

Please sign in to comment.