@@ -420,12 +420,21 @@ class C2
420420 use Counter;
421421}
422422
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();
425427
426428?>
427429]]>
428430 </programlisting >
431+ &example.outputs;
432+ <screen >
433+ <![CDATA[
434+ 1
435+ 1
436+ ]]>
437+ </screen >
429438 </example >
430439 <example xml : id =" language.oop5.traits.static.ex2" >
431440 <title >Static Methods</title >
@@ -446,41 +455,67 @@ class Example
446455 use StaticExample;
447456}
448457
449- Example::doSomething();
458+ echo Example::doSomething();
450459
451460?>
452461]]>
453462 </programlisting >
463+ &example.outputs;
464+ <screen >
465+ <![CDATA[
466+ Doing something
467+ ]]>
468+ </screen >
454469 </example >
455470 <example xml : id =" language.oop5.traits.static.ex3" >
456471 <title >Static Properties</title >
457472 <caution >
458473 <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 .
463478 </simpara >
464479 </caution >
465480 <programlisting role =" php" >
466481 <![CDATA[
467482<?php
468483
469- trait StaticExample
484+ trait T
470485{
471- public static $static = 'foo' ;
486+ public static $counter = 1 ;
472487}
473488
474- class Example
489+ class A
475490{
476- use StaticExample;
491+ use T;
492+
493+ public static function incrementCounter()
494+ {
495+ static::$counter++;
496+ }
477497}
478498
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";
480508
481509?>
482510]]>
483511 </programlisting >
512+ &example.outputs.83;
513+ <screen >
514+ <![CDATA[
515+ 2
516+ 1
517+ ]]>
518+ </screen >
484519 </example >
485520 </sect2 >
486521
@@ -562,10 +597,16 @@ class ConstantsExample {
562597}
563598
564599$example = new ConstantsExample;
565- echo $example::FLAG_MUTABLE; // 1
600+ echo $example::FLAG_MUTABLE;
566601?>
567602]]>
568603 </programlisting >
604+ &example.outputs;
605+ <screen >
606+ <![CDATA[
607+ 1
608+ ]]>
609+ </screen >
569610 </example >
570611 <para >
571612 If a trait defines a constant then a class can not define a constant with
@@ -596,8 +637,10 @@ class ConstantsExample {
596637 <title >Final methods</title >
597638 <simpara >
598639 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.
601644 </simpara >
602645 <example xml : id =" language.oop5.traits.final-methods.example" >
603646 <title >Defining a method coming from a trait as <literal >final</literal ></title >
@@ -616,20 +659,24 @@ trait CommonTrait
616659class FinalExampleA
617660{
618661 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
621663 }
622664}
623665
624666class FinalExampleB extends FinalExampleA
625667{
626- public function method() {} // Fatal error: Cannot override final method
627- // FinalExampleA::method()
668+ public function method() {}
628669}
629670
630671?>
631672]]>
632673 </programlisting >
674+ &example.outputs.similar;
675+ <screen >
676+ <![CDATA[
677+ Fatal error: Cannot override final method FinalExampleA::method() in ...
678+ ]]>
679+ </screen >
633680 </example >
634681 </sect2 >
635682
0 commit comments