diff --git a/language/oop5/traits.xml b/language/oop5/traits.xml new file mode 100644 index 0000000000..2e03794adb --- /dev/null +++ b/language/oop5/traits.xml @@ -0,0 +1,99 @@ + + + + Traits + + + Traits is a mechanism for code reuse in single inheritance languages such as + PHP. A Trait is intended to reduce some limitations of single inheritance by + enabling a developer to reuse sets of methods freely in several independent + classes living in different class hierarchies. The semantics of the combination + of Traits and classes is defined in a way which reduces complexity, and avoids + the typical problems associated with multiple inheritance and Mixins. + + + A Trait is similar to a class, but only intended to group functionality in a + fine-grained and consistent way. It is not possible to instantiate a Trait on + its own. It is an addition to traditional inheritance and enables horizontal + composition of behavior; that is, the application of class members without + requiring inheritance. + + + + Precedence + + 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 return override inherited methods. + + + + + Multiple Traits + + Multiple Traits can be inserted into a class by listing them in the use + statement, separated by commas. + + + + + Conflict Resolution + + If two Traits insert a member by the same name, a fatal error is produced, + if the conflict is not explicitly resolved. + + + To resolve conflicted naming among Traits, one can use the insteadof + operator to explicitly define the precedence between said Traits. Or, + one can use the as operator to alias the conflicting + member name(s). + + + + + &reftitle.examples; + + Trait example + + +]]> + + + + + +