|
Object InterfacesObject interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. Interfaces are defined in the same way as a class, but with the interface keyword replacing the class keyword and 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. implementsTo implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.
ConstantsIt's possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits them. ExamplesExample #1 Interface example
<?php Example #2 Extendable Interfaces
<?php Example #3 Multiple interface inheritance
<?php Example #4 Interfaces with constants
<?php An interface, together with type-hinting, provides a good way to make sure that a particular object contains particular methods. See instanceof operator and type hinting. |