Do PHP interfaces have properties?

PhpOopInterfaceMethods

Php Problem Overview


Do interfaces in PHP have properties, or do they only have methods?

Php Solutions


Solution 1 - Php

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/set or add/remove accessors).

Update: Since PHP does not have properties in the sense of get/set accessors, then the answer to your question is no. Interfaces cannot carry their own data/state.

Solution 2 - Php

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.

/**
 * @property string $password
 * @property string $username
 */
interface IUserDocument
{


}

Solution 3 - Php

Interfaces in PHP may only contain public method signatures without a method body. They may also contain constants. But that's it. Nothing else.

See 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.

Solution 4 - Php

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.

Solution 5 - Php

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.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnever_had_a_nameView Question on Stackoverflow
Solution 1 - PhpWill VousdenView Answer on Stackoverflow
Solution 2 - PhpJosef SáblView Answer on Stackoverflow
Solution 3 - PhpGordonView Answer on Stackoverflow
Solution 4 - PhpJohn FlatnessView Answer on Stackoverflow
Solution 5 - PhpMichaelView Answer on Stackoverflow