PHP 接口有属性吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2756974/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do PHP interfaces have properties?
提问by never_had_a_name
Do interfaces in PHP have properties, or do they only have methods?
PHP 中的接口有属性,还是只有方法?
采纳答案by Will Vousden
It depends what you mean by "properties". If you mean actual fields, then no, they don't. If you're referring to properties such as those in C#, then yes they can (since the property accessors are strictly syntactic sugar for accessor methods anyway). The same goes for events (though of course, in each case, no implementation is specified for the get/setor add/removeaccessors).
这取决于您所说的“属性”是什么意思。如果您的意思是实际字段,那么不,它们不是。如果您指的是 C# 中的属性,那么它们可以(因为属性访问器无论如何都是访问器方法的严格语法糖)。事件也是如此(当然,在每种情况下,都没有为get/set或add/remove访问器指定实现)。
Update: Since PHP does not have properties in the sense of get/setaccessors, then the answer to your question is no. Interfaces cannot carry their own data/state.
更新:由于 PHP 没有get/set访问器意义上的属性,因此您的问题的答案是否定的。接口不能携带自己的数据/状态。
回答by Gordon
Interfaces in PHP may only contain public method signatureswithout a method body. They may also contain constants. But that's it. Nothing else.
PHP 中的接口可能只包含没有方法主体的公共方法签名。它们也可能包含常量。但就是这样。没有其他的。
See http://www.php.net/manual/en/language.oop5.interfaces.php
见http://www.php.net/manual/en/language.oop5.interfaces.php
Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined. […] All methods declared in an interface must be public, this is the nature of an interface. […] Its possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits it.
接口是使用 interface 关键字定义的,与标准类的方式相同,但没有定义任何方法的内容。[...] 接口中声明的所有方法都必须是公共的,这是接口的本质。[…] 接口可以有常量。接口常量的工作方式与类常量完全一样,只是它们不能被继承它的类/接口覆盖。
回答by Josef Sábl
You can declare properties in DocBlock for the interface. IDE's will then hint those properties for the interface (PhpStorm does) but this will not force the actual implementation of these fields in the implementing class. E. g.
您可以在 DocBlock 中为接口声明属性。IDE 然后会提示接口的这些属性(PhpStorm 会这样做),但这不会强制在实现类中实际实现这些字段。例如
/**
 * @property string $password
 * @property string $username
 */
interface IUserDocument
{
}
回答by John Flatness
PHP interfaces can have constants, but not properties (instance variables). If you don't need to modify your "property", you can use a constant instead.
PHP 接口可以有常量,但不能有属性(实例变量)。如果您不需要修改“属性”,则可以使用常量代替。
回答by Michael
The [valid] reason for needing properties in an interface is to specify that a DTO class has a certain aspect, e.g. IOrderable { OrderDate, OrderStatus }, IDeliverable { DeliveryAddress, Route, ... }, etc. The aspect can be used in a number of DTOs e.g. Sales Order, Work Order, Sales Invoices, etc. A DTO class can support multiple aspects, i.e. multiple inheritance which is desirable in Data Classes (but not Code Classes). Thereafter, the client of the DTO is assured it can view the DTO through that aspect (an interface contract). This pattern abides by all 5 of the SOLID principles.
在接口中需要属性的 [有效] 原因是指定一个 DTO 类具有某个方面,例如 IOrderable { OrderDate, OrderStatus }, IDeliverable { DeliveryAddress, Route, ... } 等。该方面可以用于许多 DTO,例如销售订单、工作订单、销售发票等。一个 DTO 类可以支持多个方面,即数据类(但不是代码类)中需要的多重继承。此后,DTO 的客户端确保它可以通过该方面(接口合同)查看 DTO。这种模式遵守所有 5 条 SOLID 原则。
In PHP the closest you have to interface properties is traits http://php.net/manual/en/language.oop5.traits.php. Similar to interfaces, traits cannot be instantiated, however can be used directly in classes without implementing them.
在 PHP 中,最接近接口属性的是 traits http://php.net/manual/en/language.oop5.traits.php。与接口类似,特征不能被实例化,但可以直接在类中使用而无需实现它们。

