Understanding Interface in PHP

understanding interface and its practical implementation in php

An interface and class are similar but interface cannot contain code inside it. An interface can define method names and arguments, but without the contents inside the methods. Any classes implementing an interface must implement all methods defined by the interface. All methods declared in an interface must be public; this is simply the nature of an interface.

Interfaces are my absolute favorite feature of Object-Oriented Languages. The use of interface becomes very helpful because it helps to organize your code. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can’t do anything. It’s just a pattern.

an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation. Interface can extend or a class can implement multiple other interfaces.

Interface syntax for Implementation

We can declare an interface with interface keyword. Any class that inherits the interface must use implements keyword. A class can inherit multiple interfaces by separating each with comma(,).

Practical Implementation of Interface

Let’s do some practical implementation of interface on our code. Suppose we have an interface Bird.

This is a simple interface with its method signature canFly, and canSwim. Remember that interface doesn’t contain body but only name, visibility and parameters. Visibility should always be public.

Now, when we implement the interface we uses as below:

If you implement a interface, you must satisfy all its methods. For example, we use canFly and canSwim method in Duck class. Otherwise, it will display an error message.

An Interface is provided so you can describe a set of functions and then hide the final implementation of those functions in an implementing class. This allows you to change the implementation of those functions without changing how you use it.

For example I have a database. I want to write a class that accesses the data in my database. I define an interface like this:

Then let’s say, we started using MYSQL database. Then, we can write a class to access MYSQL database:

we write these methods as needed to get to the MySQL database tables. Then you can write your controller to use the interface as such:

Let us define another class for accessing MongoDB database instead of MYSQL as like:

Then, we decided to switch from MYSQL to MongoDB. To switch our application to use the MongoDB database instead of the MySQL database we only have to change ONE LINE of code:

all other lines of code, such as:

will remain unchanged. The point is – the Interface describes the methods that we need to access our database. It does NOT describe in any way HOW we achieve that. That’s what the implementing class does. We can implement this interface as many times as we need in as many different ways as we need. We can then switch between implementations of the interface without impact to our code because the interface defines how we will use it regardless of how it actually works.