[DOC-CVS] [doc-en] master: Document static asymmetric visibility. (#4891)
[email protected] (Larry Garfield via GitHub)
| Newsgroups | php.doc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: Larry Garfield (Crell) Committer: GitHub (web-flow) Pusher: Crell Date: 2026-01-09T12:07:38-06:00 Commit: https://github.com/php/doc-en/commit/b9af4bd4eef9a501e9a6787d5d8998bce9f37dfb Raw diff: https://github.com/php/doc-en/commit/b9af4bd4eef9a501e9a6787d5d8998bce9f37dfb.diff Document static asymmetric visibility. (#4891) * Document static asymmetric visibility. * Formatting fixes Co-authored-by: Gina Peter Banyard <[email protected]> * Add output. * Add marker Co-authored-by: Kamil Tekiela <[email protected]> * Fix example structure. * Whitespace Co-authored-by: Kamil Tekiela <[email protected]> --------- Co-authored-by: Gina Peter Banyard <[email protected]> Co-authored-by: Kamil Tekiela <[email protected]> Changed paths: M language/oop5/visibility.xml Diff: diff --git a/language/oop5/visibility.xml b/language/oop5/visibility.xml index 465f7f87fe5c..93418b7a0380 100644 --- a/language/oop5/visibility.xml +++ b/language/oop5/visibility.xml @@ -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 @@ -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>