How to use PHP Interface, Abstract and inheritance

How to use PHP Interface, Abstract and inheritance

The interface, Abstract and inheritance are the very important concept of oops that will help to design the application perfectly. First let discss the difference between abstract class and interface then I will explain how to implemennt.

  • Interface support multiple inheritance while abstract does not.
  • Interface does not contains constructor while abstract class can contain constructor.
  • Interface contains only signature of method while abstract class can contain signature and also complete method
  • Interface method can not be static while complete method can be static in abstract class.

When the developer received the requirements from the client then it divide the large task into small task so that we can deliver the solution within budget and time. Each task assigned to different develpers, when each develper done his task then we need to merge each task so that we can provide solution to problem. Let me explain with the help of example The Sr. developer received requiremnts from the client to calculate the salary of the emplayee. Here are the steps to solve this problem with the help of interface , abstract and inheritance concept.

  • Step 1 : Team analysed the requirements.
  • Step 2: Divide the requiremnts into small tasks, let we divde the requirements into 3 small tasks task1 , task2 task3
  • Step 3 : task 1 : provide basic info and implementation for the employee, Task 2: provide information what is required to calculate the salary, Task 3 : provide list of employee with salary.
  • Steps4 : Task 1 will implemted by the developer 1 with abstract class concept because it not completed class. Task 2 will implement with help of interface concept because it tell what is required to calculate the salary . Task 3 : implemted withe help inheritance concept that extend abastact class and implements interface.
// File name : employee.php
abstract class Employee
{
protected $id ;
protected $name;
protected $hourlyRate;
protected $grandSalary;
public function __construct($id ,$name,$hourlyRate)
{
	$this->id = $id;
	$this->name =$name;
	$this->hourlyRate =$hourlyRate;
}

public function basicSalary($weeklyHours)
{
  $this->grandSalary =  $this->hourlyRate * $weeklyHours;
}
	
}
// File name :  employee_interface.php
interface EmployeeInterface
{
	public function getRecord();
	public function  getHoursWorked($weeklyHours);
	public function  getDeduction($deduction);
	public function  salaryCalculation();
	
}

require_once('employee.php');
require_once('employee_interface.php');
// File name :  employee_details.php

Class EmployeeDetails extends Employee implements EmployeeInterface
{
private $weeklyHours;
private $deductionPct;
private $deductionAmt;
private $netSalary;
public function __construct($id ,$name,$hourlyRate)
{
	parent::__construct($id ,$name,$hourlyRate);
}


public function getHoursWorked($weeklyHours)
{
$this->weeklyHours = $weeklyHours;
}
public function getDeduction($deductionPct)
{
 $this->deductionPct = $deductionPct;
}

public function salaryCalculation ()
{
 parent::basicSalary($this->weeklyHours);
 $this->deductionAmt = ($this->deductionPct * $this->grandSalary ) /100;
 $this->netSalary = $this->grandSalary - $this->deductionAmt;
}	
public function getRecord()
{
echo '<tr>';
echo '<td>',$this->id,'</td>';
echo '<td>',$this->name,'</td>';
echo '<td>$',$this->hourlyRate,'</td>';
echo '<td>$',$this->grandSalary,'</td>';
echo '<td>$',$this->deductionAmt,'</td>';
echo '<td>$',$this->netSalary,'</td>';
echo '</tr>';
}	
	
}

$objEmplyee1 = new EmployeeDetails(100,'Baljit Singh',15);
$objEmplyee1->getHoursWorked(40);
$objEmplyee1->getDeduction(5);
$objEmplyee1->salaryCalculation();
echo '<table border =1>';
echo '<tr><td>Id</td><td>Name</td><td>Hourly Rate	</td><td>Grand Salary</td><td>Deduction</td><td>Net Salary</td></tr>';
$objEmplyee1->getRecord();
echo '</table>'



Tags