Using Interface in PHP

What is an interface in PHP? An interface is an important concept in an object oriented programming.

In this tutorial, we’ll discuss following points.

1. What is an interface?

2. How to create an interface in PHP.

3. What’s the importance of using an interface in our PHP application?

Interface in PHP

Interface in PHP

What is an Interface in PHP?

An Interface defines a contract for what a class can do, without saying anything about how the class can do it. Through interfaces, we define what a class can do, the classes that implement interface define how it can do it.

To understand the concept of an interface let’s take an example of a caching system. There are different types of caching available such as File system, Memcache, Redis etc. First, we need to define what a cache can do (I mean different operations), how we do it? we can do it through an interface. A class (Such as Memcache, Redis etc) which implements cache interface will define how they will do it.

NOTE – An interface will not add any additional functionality in your code but it outlines a standard format to which your classes need to use.

Learn PHP programming online – Video tutorials 

How to Create & Implement an Interface in PHP

i) An Interface can be created through interface keyword followed by an interface name.

ii) In an interface, methods are not defined, the class which implements must define all the methods of an interface. In short, All the methods in an interface is abstract.

iii) All the methods declare in an interface should be public.  A class can implement multiple interfaces whereas a class can extend only one abstract class.

Interface Vs Abstract Class in PHP

iv) In an interface, we can define constants. It works exactly like class constants except these constants cannot be overridden by a class/interface that inherits them.

PHP Traits

Let’s create a cache interface which has three methods such as setKey, getKey and deleteKey. Any class which implements Cache interface, define these methods.

To use an interface in a class, implements keyword is used.

NOTE – All the methods of an interface must be defined by a class which implements them.

 

Interface can extend other Interfaces

An interface can extend other interfaces. It can be extended like classes using extends operator.

A class can implement multiple interfaces

 

Some predefined interfaces in PHP.

PHP 7 Features

Why We Use Interfaces in our Code?

i) An interface allows us to hide an implementation detail.

ii) An interface is used to implement multiple behaviors into a single class (build complex types)

In next tutorials, I’ll discuss Dependency injection and interface type hinting in our code.

Conclusion

The use of Interfaces,  Abstract Classes,  Traits in our Application is not mandatory but it allows you to manage code and keep it well structured.

PHP Object-Oriented Programming Books

PHP OOPs Concept

PHP Books on Amazon India

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.