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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Defined cacheInterface interface cacheInterface { public function get($key); pubic function set($key,$value); } /* *A Memcache class implements cacheInterface */ class Memcache implements cacheInterface { public function get($key){ // method body } public function set($key,$value){ // method body } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
abstract class car { abstract public function modelType(); public function wheelCount() { echo "I have four wheels"; } } class HondaCity extends car{ public function modelType(){ echo "HondaCity"; } } $data = new HondaCity; $data->modelType(); $data->wheelCount(); |
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.
1 2 3 4 5 6 |
interface A { } interface B { } interface C { } //A class can implements multiple interfaces class Base implements A, B, C { } |
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.