@@ -420,12 +420,21 @@ class C2
420
420
use Counter;
421
421
}
422
422
423
- $o = new C1(); $o->inc(); // echo 1
424
- $p = new C2(); $p->inc(); // echo 1
423
+ $o = new C1();
424
+ $o->inc();
425
+ $p = new C2();
426
+ $p->inc();
425
427
426
428
?>
427
429
]]>
428
430
</programlisting >
431
+ &example.outputs;
432
+ <screen >
433
+ <![CDATA[
434
+ 1
435
+ 1
436
+ ]]>
437
+ </screen >
429
438
</example >
430
439
<example xml : id =" language.oop5.traits.static.ex2" >
431
440
<title >Static Methods</title >
@@ -446,41 +455,67 @@ class Example
446
455
use StaticExample;
447
456
}
448
457
449
- Example::doSomething();
458
+ echo Example::doSomething();
450
459
451
460
?>
452
461
]]>
453
462
</programlisting >
463
+ &example.outputs;
464
+ <screen >
465
+ <![CDATA[
466
+ Doing something
467
+ ]]>
468
+ </screen >
454
469
</example >
455
470
<example xml : id =" language.oop5.traits.static.ex3" >
456
471
<title >Static Properties</title >
457
472
<caution >
458
473
<simpara >
459
- Prior to PHP 8.3.0, static properties defined in a trait were shared across a single hierarchy of classes
460
- which use the trait. As of PHP 8.3.0, the static property of the trait inserted in the child class
461
- overrides the static property that the child class inherited from the parent class defined in conjunction with
462
- the same trait .
474
+ Prior to PHP 8.3.0, static properties defined in a trait were shared
475
+ across all classes in the same inheritance hierarchy which used that trait.
476
+ As of PHP 8.3.0, if a child class uses a trait with a static property,
477
+ it will be considered distinct from the one defined in the parent class .
463
478
</simpara >
464
479
</caution >
465
480
<programlisting role =" php" >
466
481
<![CDATA[
467
482
<?php
468
483
469
- trait StaticExample
484
+ trait T
470
485
{
471
- public static $static = 'foo' ;
486
+ public static $counter = 1 ;
472
487
}
473
488
474
- class Example
489
+ class A
475
490
{
476
- use StaticExample;
491
+ use T;
492
+
493
+ public static function incrementCounter()
494
+ {
495
+ static::$counter++;
496
+ }
477
497
}
478
498
479
- echo Example::$static;
499
+ class B extends A
500
+ {
501
+ use T;
502
+ }
503
+
504
+ A::incrementCounter();
505
+
506
+ echo A::$counter, "\n";
507
+ echo B::$counter, "\n";
480
508
481
509
?>
482
510
]]>
483
511
</programlisting >
512
+ &example.outputs.83;
513
+ <screen >
514
+ <![CDATA[
515
+ 2
516
+ 1
517
+ ]]>
518
+ </screen >
484
519
</example >
485
520
</sect2 >
486
521
@@ -562,10 +597,16 @@ class ConstantsExample {
562
597
}
563
598
564
599
$example = new ConstantsExample;
565
- echo $example::FLAG_MUTABLE; // 1
600
+ echo $example::FLAG_MUTABLE;
566
601
?>
567
602
]]>
568
603
</programlisting >
604
+ &example.outputs;
605
+ <screen >
606
+ <![CDATA[
607
+ 1
608
+ ]]>
609
+ </screen >
569
610
</example >
570
611
<para >
571
612
If a trait defines a constant then a class can not define a constant with
@@ -596,8 +637,10 @@ class ConstantsExample {
596
637
<title >Final methods</title >
597
638
<simpara >
598
639
As of PHP 8.3.0, the <link linkend =" language.oop5.final" >final</link >
599
- modifier can be applied to methods imported from traits by using the <literal >as</literal > operator,
600
- which permits modifying the method visibility originating from a trait in the class the trait is used in.
640
+ modifier can be applied using the <literal >as</literal > operator
641
+ to methods imported from traits. This can be used to prevent child classes
642
+ from overriding the method. However, the class that uses the trait can still
643
+ override the method.
601
644
</simpara >
602
645
<example xml : id =" language.oop5.traits.final-methods.example" >
603
646
<title >Defining a method coming from a trait as <literal >final</literal ></title >
@@ -616,20 +659,24 @@ trait CommonTrait
616
659
class FinalExampleA
617
660
{
618
661
use CommonTrait {
619
- CommonTrait::method as final; // The 'final' will prevents child classes
620
- // from overriding a method
662
+ CommonTrait::method as final; // The 'final' prevents child classes from overriding the method
621
663
}
622
664
}
623
665
624
666
class FinalExampleB extends FinalExampleA
625
667
{
626
- public function method() {} // Fatal error: Cannot override final method
627
- // FinalExampleA::method()
668
+ public function method() {}
628
669
}
629
670
630
671
?>
631
672
]]>
632
673
</programlisting >
674
+ &example.outputs.similar;
675
+ <screen >
676
+ <![CDATA[
677
+ Fatal error: Cannot override final method FinalExampleA::method() in ...
678
+ ]]>
679
+ </screen >
633
680
</example >
634
681
</sect2 >
635
682
0 commit comments