Skip to content
Open
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
38 changes: 37 additions & 1 deletion language/oop5/visibility.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ $obj2->printHello(); // Shows Public2, Protected2, Undefined
<sect3 xml:id="language.oop5.visibility-members-aviz">
<title>Asymmetric Property Visibility</title>
<simpara>
As of PHP 8.4, properties may also have their
As of PHP 8.4, object properties may also have their
visibility set asymmetrically, with a different scope for
reading (<literal>get</literal>) and writing (<literal>set</literal>).
Specifically, the <literal>set</literal> visibility may be
Expand Down Expand Up @@ -122,6 +122,42 @@ $b->pubYear = 2023; // Fatal Error
]]>
</programlisting>
</example>
<simpara>
As of PHP 8.5, <literal>set</literal> visibility may also be applied to static properties of classes.
</simpara>
<example>
<title>Asymmetric Static Property Visibility</title>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
class Manager
{
public private(set) static int $calls = 0;

public function doAThing(): string
{
self::$calls++;
// Do other stuff.
return "some string";
}
}

$m = new Manager();

$m->doAThing(); // Works
echo Manager::$calls; // Works
Manager::$calls = 5; // Fatal error
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
1
Fatal error: Uncaught Error: Cannot modify private(set) property Manager::$calls from global scope in /some/file.php
]]>
</screen>
</example>
<para>There are a few caveats regarding asymmetric visibility:</para>
<itemizedlist>
<listitem>
Expand Down