Tutorial

Abstract Class vs Interface in PHP

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.

[gads]

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.

[cc lang=”php”]
// 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
[/cc]

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 [code]generateSalary()[/code].

[cc lang=”php”]
// Base Class
abstract class Employee {
abstract public function generateSalary();
}
[/cc]

Now as per our second sentence our all four employee class should be extending our base class which is [code]Employee[/code]. 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
[cc lang=”php”]
class Developer extends Employee implements fixBug { }
[/cc]

Some Employees like QA are capable of Reporting Bug
[cc lang=”php”]
class QA extends Employee implements reportBug { }
[/cc]

Some Employees like Mngt are capable of taking long Vacation
[cc lang=”php”]
class Mngt extends Employee implements longVacation { }
[/cc]

Some Employees like Project Manager are capable of fixing bug and taking long vacation
[cc lang=”php”]
class ProjectManager extends Employee implements fixBug, longVacation { }
[/cc]

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.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *