language/oop5/traits.xml
7341710fa635b4a969bc855a1e4619cdfa6b63f1
...
...
@@ -3,7 +3,7 @@
3
3
<sect1 xml:id="language.oop5.traits" xmlns="http://docbook.org/ns/docbook">
4
4
<title>Traits</title>
5
5
<para>
6
-
As of PHP 5.4.0, PHP implements a method of code reuse called Traits.
6
+
PHP implements a way to reuse code called Traits.
7
7
</para>
8
8
<para>
9
9
Traits are a mechanism for code reuse in single inheritance languages such as
...
...
@@ -127,7 +127,7 @@ Hello Universe!
127
127
<sect2 xml:id="language.oop5.traits.multiple">
128
128
<title>Multiple Traits</title>
129
129
<para>
130
-
Multiple Traits can be inserted into a class by listing them in the use
130
+
Multiple Traits can be inserted into a class by listing them in the <literal>use</literal>
131
131
statement, separated by commas.
132
132
</para>
133
133
<example xml:id="language.oop5.traits.multiple.ex1">
...
...
@@ -239,13 +239,6 @@ class Aliased_Talker {
239
239
]]>
240
240
</programlisting>
241
241
</example>
242
-
<note>
243
-
<para>
244
-
Prior to PHP 7.0, defining a property in a class with the same name as in a trait
245
-
would throw an <constant>E_STRICT</constant> if the class definition was compatible
246
-
(same visibility and initial value).
247
-
</para>
248
-
</note>
249
242
</sect2>
250
243

251
244
<sect2 xml:id="language.oop5.traits.visibility">
...
...
@@ -332,12 +325,14 @@ Hello World!
332
325
<title>Abstract Trait Members</title>
333
326
<para>
334
327
Traits support the use of abstract methods in order to impose requirements
335
-
upon the exhibiting class.
328
+
upon the exhibiting class. Public, protected, and private methods are supported.
329
+
Prior to PHP 8.0.0, only public and protected abstract methods were supported.
336
330
</para>
337
331
<caution>
338
332
<simpara>
339
-
A concrete class fulfills this requirement by defining a concrete method
340
-
with the same name; its signature may be different.
333
+
As of PHP 8.0.0, the signature of a concrete method must follow the
334
+
<link linkend="language.oop.lsp">signature compatibility rules</link>.
335
+
Previously, its signature might be different.
341
336
</simpara>
342
337
</caution>
343
338
<example xml:id="language.oop5.traits.abstract.ex1">
...
...
@@ -371,8 +366,14 @@ class MyHelloWorld {
371
366
<sect2 xml:id="language.oop5.traits.static">
372
367
<title>Static Trait Members</title>
373
368
<para>
374
-
Traits can define both static members and static methods.
369
+
Traits can define static variables, static methods and static properties.
375
370
</para>
371
+
<note>
372
+
<para>
373
+
As of PHP 8.1.0, calling a static method, or accessing a static property directly on a trait is deprecated.
374
+
Static methods and properties should only be accessed on a class using the trait.
375
+
</para>
376
+
</note>
376
377
<example xml:id="language.oop5.traits.static.ex1">
377
378
<title>Static Variables</title>
378
379
<programlisting role="php">
...
...
@@ -420,6 +421,24 @@ Example::doSomething();
420
421
]]>
421
422
</programlisting>
422
423
</example>
424
+
<example xml:id="language.oop5.traits.static.ex3">
425
+
<title>Static Properties</title>
426
+
<programlisting role="php">
427
+
<![CDATA[
428
+
<?php
429
+
trait StaticExample {
430
+
public static $static = 'foo';
431
+
}
432
+

433
+
class Example {
434
+
use StaticExample;
435
+
}
436
+

437
+
echo Example::$static;
438
+
?>
439
+
]]>
440
+
</programlisting>
441
+
</example>
423
442
</sect2>
424
443

425
444
<sect2 xml:id="language.oop5.traits.properties">
...
...
@@ -448,10 +467,8 @@ $example->x;
448
467
</example>
449
468
<para>
450
469
If a trait defines a property then a class can not define a property with
451
-
the same name unless it is compatible (same visibility and initial value),
452
-
otherwise a fatal error is issued.
453
-
Before PHP 7.0.0, defining a property in the class with the same visibility
454
-
and initial value as in the trait, raised an E_STRICT notice.
470
+
the same name unless it is compatible (same visibility and type,
471
+
readonly modifier, and initial value), otherwise a fatal error is issued.
455
472
</para>
456
473
<example xml:id="language.oop5.traits.properties.conflicts">
457
474
<title>Conflict Resolution</title>
...
...
@@ -460,13 +477,17 @@ $example->x;
460
477
<?php
461
478
trait PropertiesTrait {
462
479
public $same = true;
463
-
public $different = false;
480
+
public $different1 = false;
481
+
public bool $different2;
482
+
public bool $different3;
464
483
}
465
484

466
485
class PropertiesExample {
467
486
use PropertiesTrait;
468
-
public $same = true; // Allowed as of PHP 7.0.0; E_STRICT notice formerly
469
-
public $different = true; // Fatal error
487
+
public $same = true;
488
+
public $different1 = true; // Fatal error
489
+
public string $different2; // Fatal error
490
+
readonly protected bool $different3; // Fatal error
470
491
}
471
492
?>
472
493
]]>
...
...
@@ -474,8 +495,57 @@ class PropertiesExample {
474
495
</example>
475
496
</sect2>
476
497

477
-
</sect1>
498
+
<sect2 xml:id="language.oop5.traits.constants">
499
+
<title>&Constants;</title>
500
+
<para>
501
+
Traits can, as of PHP 8.2.0, also define constants.
502
+
</para>
503
+
<example xml:id="language.oop5.traits.constants.example">
504
+
<title>Defining Constants</title>
505
+
<programlisting role="php">
506
+
<![CDATA[
507
+
<?php
508
+
trait ConstantsTrait {
509
+
public const FLAG_MUTABLE = 1;
510
+
final public const FLAG_IMMUTABLE = 5;
511
+
}
512
+

513
+
class ConstantsExample {
514
+
use ConstantsTrait;
515
+
}
516
+

517
+
$example = new ConstantsExample;
518
+
echo $example::FLAG_MUTABLE; // 1
519
+
?>
520
+
]]>
521
+
</programlisting>
522
+
</example>
523
+
<para>
524
+
If a trait defines a constant then a class can not define a constant with
525
+
the same name unless it is compatible (same visibility, initial value, and
526
+
finality), otherwise a fatal error is issued.
527
+
</para>
528
+
<example xml:id="language.oop5.traits.constants.conflicts">
529
+
<title>Conflict Resolution</title>
530
+
<programlisting role="php">
531
+
<![CDATA[
532
+
<?php
533
+
trait ConstantsTrait {
534
+
public const FLAG_MUTABLE = 1;
535
+
final public const FLAG_IMMUTABLE = 5;
536
+
}
478
537

538
+
class ConstantsExample {
539
+
use ConstantsTrait;
540
+
public const FLAG_IMMUTABLE = 5; // Fatal error
541
+
}
542
+
?>
543
+
]]>
544
+
</programlisting>
545
+
</example>
546
+
</sect2>
547
+

548
+
</sect1>
479
549
<!-- Keep this comment at the end of the file
480
550
Local variables:
481
551
mode: sgml
482
552