PHP中的Traits有哪些可能的使用场景? [英] What are possible use scenarios for Traits in PHP?

查看:109
本文介绍了PHP中的Traits有哪些可能的使用场景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
php中的特征–任何现实示例/最佳实践?

在哪种情况下,人们会在PHP中使用Traits?我确实对此有一个很好的整体想法,但是我似乎想不出一种在我编写的应用程序中使用它们的方法,但这可能是因为它当时不需要特性.

我已经意识到需要特质的一种情况:

  • 事件.与其让一个类实现观察者模式并让所有其他类继承该模式,不如让它成为一个特征,让想要触发事件或订阅该特征的类使用它.例如,Yii框架通过在CComponent类上实现而不是使用Trait来实现错误.

基本上的功能可以在类之间共享,但是可能在多个类层次结构中传播,应该使用特征.除了事件系统,还有哪些其他方案可以利用特质?

解决方案

特质解决的问题与Java使用接口解决的问题类似-如何在不在内部的类之间强制执行常见行为(以接口表示)相同的类层次结构.

对于仅具有继承性的语言(例如C ++),要在相同上下文中使用来自两个不同类的两个对象并要求相同的行为,则这两个类必须来自相同的层次结构.有时,这意味着创建完全人为的层次结构,只是为了允许来自不同类的对象在同一上下文中使用.

Java通过接口解决了这个问题-接口本质上是一种控制行为提供的契约,因此一个类的对象可以替代单独类的对象,因为它承诺相同的行为-接口.但是它们不必来自相同的层次结构.

PHP特性体现了这个想法.特征是一种接口,是类包含的一组行为,因此可以在需要该行为的上下文中使用.因此,任何Java接口示例都应继承到PHP特性示例.但是,PHP特性与Java接口有些不同,因为特性可以包含完整的函数定义,而Java接口只能包含声明(典型的PHP特性!)

Possible Duplicate:
traits in php – any real world examples/best practices?

In what kind of situations would one use Traits in PHP? I do have a pretty good overall idea of this, but I can't seem to think of a way to use them in an application I have written, but that may be because it does not need traits at the time.

One scenario I have realized that needs traits:

  • Events. Instead of having one class that implements the observer pattern and letting all other classes inheriting it, just make it a trait and let classes that want to fire events or subscribe to use the trait. For example, the Yii framework is doing it wrong by implementing stuff at CComponent class rather than using a Trait.

Basically functionality that can be shared among classes, but may spread along multiple class hierarchies should use traits. What other scenarios could take advantage of Traits than an event system?

解决方案

The issue that Traits addresses is similar to the one that Java addresses with interfaces - how to enforce common behaviour (as represented by interfaces) among classes that are not in the same class hierarchy.

With languages such as C++ which only have inheritance, for two objects from two different classes to be used in the same context requiring the same behaviour, the two classes had to be from the same hierarchy. This sometimes meant creating quite artificial hierarchies simply to allow objects from different classes to be used in the same context.

Java tackled this problem through interfaces - an interface is essentially a contract governing the provision of behaviour so that an object of one class can be substituted for an object of a separate class because it promises the same behaviour - the interface. But they don't have to be from the same hierarchy.

PHP Traits embody this idea. A trait is a kind of interface, a set of behaviours that a class contains so that it can be used in a context that requires that behaviour. So, any Java interface example should carry over to a PHP Traits example. PHP Traits are a bit different to Java interfaces, though, since Traits can contain full function definitions, whereas Java interfaces can only contain declarations (typical PHP idiosyncrasy!)

这篇关于PHP中的Traits有哪些可能的使用场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆