How to Create and Implement an Interface in PHP


In this article, we show how to create and implement an interface in PHP.

An interface is the equivalent of an agreement to implement certain methods.

So in an interface we declare certain methods.

If a class implements an interface, it must implement all the methods set up in the interface.

So you can see that creating an interface and then a class that implements that interface is a great way to ensure that the class carries out all of the functions defined in the interface.

To create an interface in PHP, all you must do is use the keyword interface followed by the name of the interface (any name).

When you create a class that you want to implement that interface, you simply declare the class, as you would normally would and follow that line with implements name_of_interface.

So below in the PHP code, we create an interface named calculation. The class that we then create named circle implements this interface. So this means that this circle class must implement the functions listed in the interface named calculation.

So in the code below we create an interface named calculation.

In this interface function, we define a public method named calculateArea().

We then create a class named rectangle that implements this interface, calculation.

Because this class, rectangle, implements the calculation interface, it must implement the method(s) defined in the interface. Since there is one method named calculateArea() in the calculation interface, this class rectangle must implement this calculateArea() function.

In the calculateArea() function, we echo out the formula for a rectangle.

To test the code, we create an object of the rectangle class, $rectangle1. We then invoke the calculateArea() method on the $rectangle1 object.

And the code works.

Running the above PHP code yields the following output below.

Actual PHP Output

The formula to calculate the area of a rectangle is, A= l x w

If the class that you create that implements an interface does not implement the method(s) in the interface, a fatal error will be output, such as shown below.

Fatal error: Class rectangle contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (calculation::calculateArea)

There's a few other rules when you are dealing with interfaces in PHP.

The interface itself does not implement any methods. The methods in the interface are empty methods; they contain nothing.

It's the class that implements the interface that contains a method that is defined and implements the methods.

Another rule is that the methods in an interface must be declared public. It cannot be private because other classes have to access the methods to implement. For the same reason, the method(s) defined in the interface cannot be declared final.

Another rule is that the method that you define must have the same name obviously as the method in the interface but it must also accept the same exact parameters in all classes that implement the method(s). So if a method takes no parameters in the interface, it must take no parameters in the classes that implement the interface. If the method takes one parameter named $length, for instance, it must take one parameter named $length in the classes that implement the interface.

So this is all that is required to create and implement an interface in PHP.

HTML Comment Box is loading comments...