InterviewSolution
Saved Bookmarks
| 1. |
How does the method overriding work with Traits? |
|
Answer» An inherited member from a base class is overridden by a member inserted by a Trait. The PRECEDENCE order is that members from the current class OVERRIDE Trait METHODS, which in turn override inherited methods. An inherited method from a base class is overridden by the method inserted into MyHelloWorld from the SayWorld Trait. The behaviour is the same for the methods defined in the MyHelloWorld class. The precedence order is that methods from the current class override Trait methods, which in turn override methods from the base class. trait A { function calc($v) { return $v+1; } } class MyClass { use A { calc as protected traitcalc; } function calc($v) { $v++; return $this->traitcalc($v); } } |
|