What is the point of using interfaces?

The point of using an interface has to do with code organization for me, than enforcement by the language itself. I use them a lot when preparing code for other developers to work with so that they stay within the intended design patterns. Interfaces are a kind of “design by contract” whereby your code is agreeing to respond to a prescribed set of API calls that may be coming from code you do not have access to.

Definition

  • 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.
  • Object 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 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.
  • Interfaces allow you to create code which defines the methods of classes that implement it. You cannot however add any code to those methods.
  • To implement an interface, the implements operator is used.
  • It’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.

Source: php.net

 

Leave a Reply

Your email address will not be published. Required fields are marked *