PHP Interface

Summary: in this tutorial, you will learn how to use PHP interface that is one of the most important building blocks in PHP object-oriented programming.

Introduction to PHP interface

An interface allows you to specify a list of methods that a class must implement. To define an interface, you use the interface keyword as follows:

An interface consists of methods that contain no implementation. In other words, all methods of the interface are abstract methods. An interface can have its own constants. See the following syntax:

All the methods in the interface must have public visibility level.

When we want to declare a new class that reuses properties and methods of an existing class, we say a subclass inherits from or extends a parent class. However for the interfaces, we say a class implements an interface. A class can only inherit from one class whereas a class can implement one or more interfaces.

To implement an interface, you use the  implements operator as follows:

When a class implements an interface, we also called it a concrete class. The concrete class must implement all methods the interface with the same names and signatures.

A class cannot implement two interfaces that has the a same method name because it would end up with the method ambiguity.

Like a class, you can extend an interface using the extends operator as follows:

The IWritable interface extends the IReadable interface by adding one more abstract method named write().

Why should we use PHP interfaces?

There are several reasons that we should use interface in our PHP object-oriented programming:

  • By implement an interface, the caller of the object need to care only about the object’s interface, not implementations of the object’s methods. Therefore the implementations can be changed without affecting the caller of the interface.
  • An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy.
  • An interface enables you to model multiple inheritance because a class can implement more than one interface whereas it can extend only one class.

PHP interface example

In the following example, we will show you how to use interface to make the system easier to extend and more flexible. Suppose, we have to create a logger that can log a message.

PHP Interface - ILogger Example

PHP Interface – ILogger Example

First, we create an interface called ILogger as follows:

Second, we can create a FileLogger class that writes the log messages to a log file.

Let’s create the FileLogger class:

We can use the FileLogger class as follows:

We can add a database logger that implements ILogger interface as follows:

PHP Interface ILogger Example with FileLogger and DBLogger

For the sake of demonstration, we make the DBLogger class as simple as possible.

We can easily add other kinds of loggers that implement the ILogger interface without touching existing loggers.

The following code snippet demonstrates how to use multiple loggers at the same time using a single ILogger interface:

In this tutorial, we have shown you how to use PHP interface, which is one of the most important building blocks in PHP object-oriented programming.

  • Was this tutorial helpful ?
  • YesNo