Using interfaces

Interfaces are useful tools for systems architects and are often used to prototype an Application Programming Interface (API). Interfaces don't contain actual code, but can contain names of methods as well as method signatures.


All methods identified in the Interface have a visibility level of public.

How to do it...

  1. Methods identified by the interface cannot contain actual code implementations. You can, however, specify the data types of method arguments.
  2. In this example, ConnectionAwareInterface identifies a method, setConnection(), which requires an instance of Connection as an argument:
    interface ConnectionAwareInterface
      public function setConnection(Connection $connection);
  3. To use the interface, add the keyword implements ...

Get PHP 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.