PHP 特征的 UML 表示

UML representation of PHP trait

我正在使用 Symfony2/Doctrine 创建项目并尝试实现特征。 到目前为止,在小型试用中没有问题,但我通常在深入复杂项目之前先做 UML class 和序列图。

用来表示 PHP 特征的 UML 设计对象是什么,据我所知,这些特征可以看作是行为?有什么干净的方法吗?

非常感谢您的回答!

尼古拉斯

在我看到 Roles/Traits 解释的最早的论文中,它们在 UML 中用一条线将 Role/Trait 连接到 class 内的 method/function 来表示. http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf

在我看来,PHP 特征只不过是您在其他语言中找到的协议定义。该协议是 class 的一些功能扩展。您通常会使用接口(Trait)和 class 对其进行建模,在其中绘制从 class 到 Trait 的 <> 关系。

PHP Trait 基本上是 UML Abstract Class or UML Class Template connected to the used-in class with the UML Generalization Relationship 利用 多重继承 符号

另请参阅:

  • 图"UML Diagram with a Trait"文章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