Abstract Class vs Interface in PHP

  • Avinash
  • 0
  • Jul 27, 2014
  • Tutorial

This can be never ending discussion for group of developers. Yes its true because at a first glance Abstract Class and Interface doing the same thing. Before reading further if you want to look in details for both concepts then you can follow links below:

  1. Abstract Class in PHP
  2. Interface in PHP

So I hope you have gone through above articles and/or you may already have detailed idea of what these two concepts means and you may have got an idea for reason behind writing this article too.

Abstract Class vs Interface in PHP

I am sure that you must have question: When to use which one?

When to use which one?

When you try to answer this question in programming way then you will end up with same place from you have started. So rather than going through programming way I will try to explain this one in different way.

Let’s say you have two modules called A and B. In simple words if you can make the word A is a B then you should be using Abstract Class but if you can make the word A is capable of doing as B then you should be using Interface.

So based on above example Interface is just general contract on behaviour/functionality between two or more modules.

Let’s say you have two modules called A and B. In simple words if you can make the word A is a B then you should be using Abstract Class but if you can make the word A is capable of doing as B then you should be using Interface.

Let me try to explain this concept with an example, Please go through below sentences first and understand those clearly:

  1. Organization has Employees
  2. Employee have Salary Associated with all of them
  3. Some Employees like Developer are capable of Fixing Bug
  4. Some Employees like QA are capable of Reporting Bug
  5. Some Employees like Mngt are capable of taking long Vacation
  6. Some Employees like Project Manager are capable of fixing bug and taking long vacation

I hope you have read above six sentences and understood those as well. Now relate these sentences with the rule I have written to make decision between Abstract Class and Interface. Based on above sentences we can define below classes and interfaces in our code.

// Classes to be defined class Employee class Developer class QA class Mngt class ProjectManager // Interfaces to be defined (These are kind of capabilities and does not bound to employees only) interface fixBug interface reportBug interface longVacation
// Classes to be defined
class Employee
class Developer
class QA
class Mngt
class ProjectManager

// Interfaces to be defined (These are kind of capabilities and does not bound to employees only)
interface fixBug
interface reportBug
interface longVacation

Now with above class and interface definition we can define that Employee is our base class and which will be extended by all types of employees, here Developer, QA, Mngt and Project Manager. Also Employee class can define/implement any method which can be applies to all types of employee. In this example Employee class will contain one abstract method which is generateSalary().

// Base Class abstract class Employee { abstract public function generateSalary(); }
// Base Class
abstract class Employee {
    abstract public function generateSalary();
}

Now as per our second sentence our all four employee class should be extending our base class which is Employee. Also all four types of employees should be implementing respective interfaces based on their capabilities.

Let us look at below code blocks based on scenarios we have:

Some Employees like Developer are capable of Fixing Bug

class Developer extends Employee implements fixBug { }
class Developer extends Employee implements fixBug { }

Some Employees like QA are capable of Reporting Bug

class QA extends Employee implements reportBug { }
class QA extends Employee implements reportBug { }

Some Employees like Mngt are capable of taking long Vacation

class Mngt extends Employee implements longVacation { }
class Mngt extends Employee implements longVacation { }

Some Employees like Project Manager are capable of fixing bug and taking long vacation

class ProjectManager extends Employee implements fixBug, longVacation { }
class ProjectManager extends Employee implements fixBug, longVacation { }

So based on above sentences we can use interface and abstract classes in above defined way. Frankly speaking, this is the best example I have come across for this topic. If you are still confuse with this topic then I would suggest leave this topic and go with what you are comfortable with because you can achieve things using any of the feature. I know this can open up a very good and healthy discussion here, so be the first and start conversion on this topic.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: