Make sense of PHP interfaces
Tags: php, oop
Published: 2020-02-22 23:03:59

Indeed PHP interfaces have a purpose. 

They are contracts, like instruction manuals for other developers.
However, it might be difficult to understand why interfaces are useful.

## Basics

An interface is an abstract class therefore you cannot instantiate it. Instead, you will implement it.

Here is a very basic example:

```php
interface MyInterface {
    public function setName(string $name);
    public function getName();
}

class MyClass implements MyInterface {
    private $name; 

    public function setName(string $name) { 
        $this->name = $name; 
    }
    
    public function getName() {
        return $this->name; 
    }
}
```

`MyClass` must implement `setName()` and `getName()`. If you don't do that, you will get a fatal error:

> Fatal error: Class MyClass contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (MyInterface::setName, MyInterface::getName)

Besides, all methods in interfaces must have public visibility scope and interfaces cannot have attributes.

## The why

Developers use interfaces to describe how a class or a collection of classes should behave.

Interfaces encapsulate the implementation without handling the details for each class. But why?

Decoupling! 

Interfaces allow you to change the implementation without changing the details a.k.a the way you use this implementation.

## More advanced example

Any cache would need at least:

* to store/set something in the cache
* to get something in the cache
* to delete something in the cache

For that purpose, we may create the following cache interface:

```php
interface CacheInterface {
    public function set(string $key, $val);
    public function get(string $key);
    public function delete(string $key);
}
```

This way, we can tell all developers to implement our cache interface without knowing the particular details of each use.

As a result, it's easy to change the caching system without changing the way it's used in the project.

## The Symfony example

The previous example makes sense. Let's look at [Symfony](https://symfony.com/). When you want to implement any cache system, it's good practice to do the following:

```php
use Symfony\Contracts\Cache\CacheInterface;

class MyClass {
    private $cache;
    public function __construct(CacheInterface $cache)
    {
        $this->cache = $cache;
    }
}
```

Here we inject the cache implementation into our class (dependency injection). Next time we want to change the cache system, we won't have to modify our class at all.

## Multiple implements vs multiple inheritances

PHP doesn’t support multiple inheritances. The following code is not possible:

```php
class MyClass extends ClassX, ClassY {}
```
This is partly because of the [Diamond problem](https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem).

Instead, you could do:

```php
class ClassY extends ClassX {}
class MyClass extends ClassY {}
```

but `ClassX` and `ClassY` might handle different things so it does not make any sense to use inheritance.

If you want to enforce multiple behaviors, you can implement multiple interfaces.
```php
class MyClass implements InterfaceX, InterfaceY {}
```

In other words, you can group classes that share some functionality but do not share a parent class.

## Wrap up

PHP interfaces are templates of method signatures for your classes. This is great for decoupling implementation and its use.

It's particularly useful when you need some flexibility while making sure all developers follow a set of rules.