Skip to content

Commit b6f7d2d

Browse files
authored
Update trait docs for PHP-8.2 (#1995)
The docs for traits probably need a good overhaul, but this documents the new constant in traits feature.
1 parent e5bdb24 commit b6f7d2d

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

language/oop5/traits.xml

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ $example->x;
466466
</example>
467467
<para>
468468
If a trait defines a property then a class can not define a property with
469-
the same name unless it is compatible (same visibility and initial value),
470-
otherwise a fatal error is issued.
469+
the same name unless it is compatible (same visibility and type,
470+
readonly modifier, and initial value), otherwise a fatal error is issued.
471471
</para>
472472
<example xml:id="language.oop5.traits.properties.conflicts">
473473
<title>Conflict Resolution</title>
@@ -476,20 +476,74 @@ $example->x;
476476
<?php
477477
trait PropertiesTrait {
478478
public $same = true;
479-
public $different = false;
479+
public $different1 = false;
480+
public bool $different2;
481+
readonly public bool $different3;
480482
}
481483
482484
class PropertiesExample {
483485
use PropertiesTrait;
484486
public $same = true;
485-
public $different = true; // Fatal error
487+
public $different1 = true; // Fatal error
488+
public string $different2; // Fatal error
489+
readonly public bool $different3; // Fatal error
486490
}
487491
?>
488492
]]>
489493
</programlisting>
490494
</example>
491495
</sect2>
492496

497+
<sect2 xml:id="language.oop5.traits.constants">
498+
<title>&Constants;</title>
499+
<para>
500+
Traits can, as of PHP 8.2.0, also define constants.
501+
</para>
502+
<example xml:id="language.oop5.traits.constants.example">
503+
<title>Defining Constants</title>
504+
<programlisting role="php">
505+
<![CDATA[
506+
<?php
507+
trait ConstantsTrait {
508+
public const FLAG_MUTABLE = 1;
509+
final public const FLAG_IMMUTABLE = 5;
510+
}
511+
512+
class ConstantsExample {
513+
use ConstantTrait;
514+
}
515+
516+
$example = new ConstantExample;
517+
$example::FLAG;
518+
?>
519+
]]>
520+
</programlisting>
521+
</example>
522+
<para>
523+
If a trait defines a constants then a class can not define a constants with
524+
the same name unless it is compatible (same visibility, initial value, and
525+
finality), otherwise a fatal error is issued.
526+
</para>
527+
<example xml:id="language.oop5.traits.constants.conflicts">
528+
<title>Conflict Resolution</title>
529+
<programlisting role="php">
530+
<![CDATA[
531+
<?php
532+
trait ConstantsTrait {
533+
public const FLAG_MUTABLE = 1;
534+
final public const FLAG_IMMUTABLE = 5;
535+
}
536+
537+
class ConstantsExample {
538+
use ConstantTrait;
539+
public const FLAG_IMMUTABLE = 5; // Fatal error
540+
}
541+
?>
542+
]]>
543+
</programlisting>
544+
</example>
545+
</sect2>
546+
493547
</sect1>
494548
<!-- Keep this comment at the end of the file
495549
Local variables:

0 commit comments

Comments
 (0)