@@ -466,8 +466,8 @@ $example->x;
466
466
</example >
467
467
<para >
468
468
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.
471
471
</para >
472
472
<example xml : id =" language.oop5.traits.properties.conflicts" >
473
473
<title >Conflict Resolution</title >
@@ -476,20 +476,74 @@ $example->x;
476
476
<?php
477
477
trait PropertiesTrait {
478
478
public $same = true;
479
- public $different = false;
479
+ public $different1 = false;
480
+ public bool $different2;
481
+ readonly public bool $different3;
480
482
}
481
483
482
484
class PropertiesExample {
483
485
use PropertiesTrait;
484
486
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
486
490
}
487
491
?>
488
492
]]>
489
493
</programlisting >
490
494
</example >
491
495
</sect2 >
492
496
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
+
493
547
</sect1 >
494
548
<!-- Keep this comment at the end of the file
495
549
Local variables:
0 commit comments