Skip to main content

PHP Interface: Why do we need that

PHP Object Interfaces

Object interfaces allow you to create code that specifies which methods a class must implement, without having to define how these methods are implemented. Interfaces share a namespace with classes and traits, so they may not use the same name.

An Interface allows the users to create programs, specifying the public methods that a class must implement, without involving the complexities and details of how the particular methods are implemented. It is generally referred to as the next level of abstraction. It resembles the abstract methods, resembling the abstract classes. An Interface is defined just like a class is defined but with the class keyword replaced by the interface keyword and just the function prototypes. The interface contains no data variables. The interface is helpful in a way that it ensures maintaining a sort of metadata for all the methods a programmer wishes to work on.

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.

In practice, interfaces serve two complementary purposes:

  • To allow developers to create objects of different classes that may be used interchangeably because they implement the same interface or interfaces. A common example is multiple database access services, multiple payment gateways, or different caching strategies. Different implementations may be swapped out without requiring any changes to the code that uses them.
  • To allow a function or method to accept and operate on a parameter that conforms to an interface, while not caring what else the object may do or how it is implemented. These interfaces are often named like IterableCacheableRenderable, or so on to describe the significance of the behavior.

Interfaces may define magic methods to require implementing classes to implement those methods.

Interfaces can be extended like classes using the extends operator.

The class implementing the interface must declare all methods in the interface with a compatible signature.

A class can implement two interfaces that define a method with the same name, only if the method declaration in both interfaces is identical.

A class that implements an interface may use a different name for its parameters than the interface. However, as of PHP 8.0 the language supports named arguments, which means callers may rely on the parameter name in the interface. For that reason, it is strongly recommended that developers use the same parameter names as the interface being implemented.

It's possible for interfaces to have constants. Interface constants work exactly like class constants. Prior to PHP 8.1.0, they cannot be overridden by a class/interface that inherits them.

Just as all interface methods are public, all interface methods are abstract as well.

In their book on Design Patterns, Erich Gamma and his associates (AKA: "The Gang of Four") use the term "interface" and "abstract class" interchangeably. In working with PHP and design patterns, the interface, while clearly a "contract" of what to include in an implementation is also a helpful guide for both re-use and making changes. As long as the implemented changes follow the interface (whether it is an interface or abstract class with abstract methods), large complex programs can be safely updated without having to re-code an entire program or module.

 

PHP Interfaces and Drupal 9

In drupal9 we can set interfaces and their implementation class, which should be located in the proper file tree and naming conventions:

  • Interface file naming and location:
    <module_name>/src/objectManagerInterface.php
  • Implementing file naming and location: 
    <module_name>/src/objectManager.php