Abstract Class vs Interface in PHP

Abstract class vs interface in PHP. What’s the Difference between an abstract class and interface in PHP? If you are doing object-oriented programming then i am sure you have heard these two terms.

In this tutorial, I am going to discuss the differences between an abstract class and interface.

Interface in PHP

An interface is an agreement or a contract. When a class implements an interface, It means it contains same public methods of an interface with their implementation.

When we create an interface in an application, we define a contract for what a class can do, without saying anything about how the class will do it.

Example –

In the above example, A cacheInterface is created with public methods get and set. Any class which implements this interface defines get and set methods.

I have created a Memcache class which are implementing a cacheInterface. It means it adheres the contract and defines the body of a get and set methods. Similarly, if any other caching class implements this interface it needs to define the get and set methods.

An Abstract Class in PHP

An Abstract class is used to define a basic skeleton or a blueprint for a child classes. A class which extends an abstract class must define some or all of it’s abstract methods.

** An abstract classes cannot be instantiated directly.

Abstract class Vs Interface in PHP

Abstract Class Vs Interface in PHP

Abstract Class Vs Interface in PHP

1. An Abstract class acts as a blueprint for a class which inherits them. A child class which inherits an abstract class needs to implement their abstract methods. An abstract class is only created for an inheritance, it means you can’t create their object directly

An interface is a contract. It only contains method signatures (Without any method body). A Class which implements interface define their body.

2. A class can extend only one abstract class whereas a class can implement multiple interfaces.

PHP traits explanation with example.

3. In an Abstract class, We can define instance variables and concrete method (non-abstract method) as well.

But in an interface all the methods are abstract. We can’t define instance variables but we can define constants in an interface.

4. An abstract class is good when there are some common features to be shared by all the objects.

Use interface when all the features implemented differently for all the objects.  All the methods of a cacheInterface will be implemented differently for all the classes.

For example – Memcache class define the body of a get and set method but their implementation is different from a Redis class.

Conclusion

I hope this post gives you the clear idea of what are the differences between an abstract class and interface. If you want to add any other points you can let us know through your comments.

Tagged , , , . Bookmark the permalink.

About WebRewrite

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