Do interfaces have properties or only methods?
Thanks
Do interfaces have properties or only methods?
Thanks
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.
In PHP, interfaces can only have abstract public methods. No constructor, no concrete methods, no properties (PHP's term for instance/static variables).
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.
Interfaces in PHP may only contain public method signatures without a method body and constants. 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.