language/oop5/basic.xml
c1f37a6c270aadbbb3da56a3973ffd62197adf2b
...
...
@@ -14,10 +14,12 @@
14
14
</para>
15
15
<para>
16
16
The class name can be any valid label, provided it is not a
17
-
PHP <link linkend="reserved">reserved word</link>. A valid class
18
-
name starts with a letter or underscore, followed by any number of
19
-
letters, numbers, or underscores. As a regular expression, it
20
-
would be expressed thus:
17
+
PHP <link linkend="reserved">reserved word</link>.
18
+
As of PHP 8.4.0, using a single underscore <literal>_</literal> as a
19
+
class name is deprecated.
20
+
A valid class name starts with a letter or underscore,
21
+
followed by any number of letters, numbers, or underscores.
22
+
As a regular expression, it would be expressed thus:
21
23
<code>^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$</code>.
22
24
</para>
23
25
<para>
...
...
@@ -139,7 +141,7 @@ Stack trace:
139
141
<classname>AllowDynamicProperties</classname> attribute. Attempting to do so
140
142
will trigger a compile-time error.
141
143
</para>
142
-
<example>
144
+
<informalexample>
143
145
<programlisting role="php">
144
146
<![CDATA[
145
147
<?php
...
...
@@ -151,14 +153,14 @@ readonly class Foo {
151
153
?>
152
154
]]>
153
155
</programlisting>
154
-
</example>
156
+
</informalexample>
155
157

156
158
<para>
157
-
As neither untyped, nor static properties can be marked with the
159
+
As neither untyped nor static properties can be marked with the
158
160
<literal>readonly</literal> modifier, readonly classes cannot declare
159
161
them either:
160
162
</para>
161
-
<example>
163
+
<informalexample>
162
164
<programlisting role="php">
163
165
<![CDATA[
164
166
<?php
...
...
@@ -183,7 +185,7 @@ readonly class Foo
183
185
?>
184
186
]]>
185
187
</programlisting>
186
-
</example>
188
+
</informalexample>
187
189
<para>
188
190
A <modifier>readonly</modifier> class can be
189
191
<link linkend="language.oop5.basic.extends">extended</link>
...
...
@@ -204,7 +206,7 @@ readonly class Foo
204
206
requirement).
205
207
</para>
206
208
<para>
207
-
If a <type>string</type> containing the name of a class is used with
209
+
If a variable containing a <type>string</type> with the name of a class is used with
208
210
<literal>new</literal>, a new instance of that class will be created. If
209
211
the class is in a namespace, its fully qualified name must be used when
210
212
doing this.
...
...
@@ -222,11 +224,16 @@ readonly class Foo
222
224
<programlisting role="php">
223
225
<![CDATA[
224
226
<?php
227
+
class SimpleClass {
228
+
}
229
+

225
230
$instance = new SimpleClass();
231
+
var_dump($instance);
226
232

227
233
// This can also be done with a variable:
228
234
$className = 'SimpleClass';
229
235
$instance = new $className(); // new SimpleClass()
236
+
var_dump($instance);
230
237
?>
231
238
]]>
232
239
</programlisting>
...
...
@@ -294,6 +301,9 @@ object(ClassD)#1 (0) {
294
301
<programlisting role="php">
295
302
<![CDATA[
296
303
<?php
304
+
class SimpleClass {
305
+
public string $var;
306
+
}
297
307

298
308
$instance = new SimpleClass();
299
309

...
...
@@ -330,26 +340,27 @@ object(SimpleClass)#1 (1) {
330
340
<programlisting role="php">
331
341
<![CDATA[
332
342
<?php
343
+

333
344
class Test
334
345
{
335
-
static public function getNew()
346
+
public static function getNew()
336
347
{
337
-
return new static;
348
+
return new static();
338
349
}
339
350
}
340
351

341
-
class Child extends Test
342
-
{}
352
+
class Child extends Test {}
343
353

344
-
$obj1 = new Test();
345
-
$obj2 = new $obj1;
354
+
$obj1 = new Test(); // By the class name
355
+
$obj2 = new $obj1(); // Through the variable containing an object
346
356
var_dump($obj1 !== $obj2);
347
357

348
-
$obj3 = Test::getNew();
358
+
$obj3 = Test::getNew(); // By the class method
349
359
var_dump($obj3 instanceof Test);
350
360

351
-
$obj4 = Child::getNew();
361
+
$obj4 = Child::getNew(); // Through a child class method
352
362
var_dump($obj4 instanceof Child);
363
+

353
364
?>
354
365
]]>
355
366
</programlisting>
...
...
@@ -372,14 +383,18 @@ bool(true)
372
383
<programlisting role="php">
373
384
<![CDATA[
374
385
<?php
375
-
echo (new DateTime())->format('Y');
386
+
echo (new DateTime())->format('Y'), PHP_EOL;
387
+

388
+
// surrounding parentheses are optional as of PHP 8.4.0
389
+
echo new DateTime()->format('Y'), PHP_EOL;
376
390
?>
377
391
]]>
378
392
</programlisting>
379
393
&example.outputs.similar;
380
394
<screen>
381
395
<![CDATA[
382
-
2016
396
+
2025
397
+
2025
383
398
]]>
384
399
</screen>
385
400
</example>
...
...
@@ -493,6 +508,14 @@ echo ($obj->bar)(), PHP_EOL;
493
508
<programlisting role="php">
494
509
<![CDATA[
495
510
<?php
511
+
class SimpleClass
512
+
{
513
+
function displayVar()
514
+
{
515
+
echo "Parent class\n";
516
+
}
517
+
}
518
+

496
519
class ExtendClass extends SimpleClass
497
520
{
498
521
// Redefine the parent method
...
...
@@ -752,9 +775,10 @@ Does\Not\Exist
752
775
namespace NS {
753
776
class ClassName {
754
777
}
778
+

779
+
$c = new ClassName();
780
+
print $c::class;
755
781
}
756
-
$c = new ClassName();
757
-
print $c::class;
758
782
?>
759
783
]]>
760
784
</programlisting>
...
...
@@ -783,7 +807,7 @@ NS\ClassName
783
807
<para>
784
808
<example>
785
809
<title>Nullsafe Operator</title>
786
-
<programlisting role="php">
810
+
<programlisting role="php" annotations="non-interactive">
787
811
<![CDATA[
788
812
<?php
789
813

790
814