language/oop5/anonymous.xml
d6f54016d62904cfd8200604aadd5e3f0d9bad97
...
...
@@ -1,12 +1,10 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-

4
3
<sect1 xml:id="language.oop5.anonymous" xmlns="http://docbook.org/ns/docbook">
5
4
<title>Anonymous classes</title>
6
5

7
6
<para>
8
-
Support for anonymous classes was added in PHP 7. Anonymous classes are
9
-
useful when simple, one-off objects need to be created.
7
+
Anonymous classes are useful when simple, one-off objects need to be created.
10
8
</para>
11
9

12
10
<informalexample>
...
...
@@ -14,7 +12,7 @@
14
12
<![CDATA[
15
13
<?php
16
14

17
-
// Pre PHP 7 code
15
+
// Using an explicit class
18
16
class Logger
19
17
{
20
18
public function log($msg)
...
...
@@ -25,7 +23,7 @@ class Logger
25
23

26
24
$util->setLogger(new Logger());
27
25

28
-
// PHP 7+ code
26
+
// Using an anonymous class
29
27
$util->setLogger(new class {
30
28
public function log($msg)
31
29
{
...
...
@@ -175,8 +173,34 @@ class@anonymous/in/oNi1A0x7f8636ad2021
175
173
</screen>
176
174
</informalexample>
177
175
</note>
178
-
</sect1>
179
176

177
+
<sect2 xml:id="language.oop5.anonymous.readonly">
178
+
<title>Readonly anonymous classes</title>
179
+
<simpara>
180
+
As of PHP 8.3.0, the <literal>readonly</literal> modifier can
181
+
be applied to anonymous classes.
182
+
</simpara>
183
+
<example xml:id="language.oop5.anonymous.readonly.example">
184
+
<title>Defining a readonly anonymous class</title>
185
+
<programlisting role="php">
186
+
<![CDATA[
187
+
<?php
188
+
// Using an anonymous class
189
+
var_dump(new readonly class('[DEBUG]') {
190
+
public function __construct(private string $prefix)
191
+
{
192
+
}
193
+

194
+
public function log($msg)
195
+
{
196
+
echo $this->prefix . ' ' . $msg;
197
+
}
198
+
});
199
+
]]>
200
+
</programlisting>
201
+
</example>
202
+
</sect2>
203
+
</sect1>
180
204
<!-- Keep this comment at the end of the file
181
205
Local variables:
182
206
mode: sgml
183
207