Interfaces

Interfaces provide a way of implementing multiple inheritance (not directly available in PHP) through the use of the keywords interface and implements.

 

Potentially, one could inherit from a parent class, and then inherit from that class and so in a chain like fashion. However, this would likely mean a number of the parents, parents, parents, etc, members might be included that might not be required, and avoids needing a very long trail like this:

class Test implements InterfaceA, InterfaceB, InterfaceC, InterfaceD, InterfaceE......InterfaceZ {}

 

Interfaces:

  • 100% abstract classes
  • Cannot be instantiated
  • Contain just the method names, not their definitions (i.e. the inner guts of their code)
  • Act like a contract to the class implementing the specified interface
  • All method names within an interface have public visibility
  • The implementing class must define the methods

The interface simply uses the keyword interface, which is then used by the class using the interface by way of the keyword implements:

Save & refresh browser:

The guy who invented big bangs: Stephen Hawking is now 71 years old!
Stephen Hawking has just made a measurement!
Stephen Hawking has written a paper! 

 

Geek::__set_state(array(
'name' => 'Stephen Hawking',
'age' => 71,
))

Leave a Reply