UML representation of PHP trait

回眸只為那壹抹淺笑 提交于 2019-11-30 15:26:03
xmojmr

PHP Trait is basically UML Abstract Class or UML Class Template connected to the used-in class with the UML Generalization Relationship utilizing the multiple inheritance notation

See also:

  • Figure "UML Diagram with a Trait" in article Brendan Bates: Traits: The Right Way

  • Programmers: Is there a representation for mixins or traits on UML?

  • PHP Manual → Language Reference → Classes and Objects → Traits

    As of PHP 5.4.0, PHP implements a method of code reuse called Traits.

    Traits are 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

In the earliest paper that I've seen Roles/Traits explained, they are represented in UML with a line connecting the Role/Trait to the method/function inside the class. http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf

As I see it a PHP trait is nothing else than a protocol definition you find in other languages. The protocol is some functional extension to the class. You would usually model that with an interface (the Trait) and the class using it where you draw a <> relation from the class to the Trait.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!