PHP5 Interfaces

An interface simply lists (function prototype) the methods a class must implement. However, in PHP a class can implement multiple interfaces and these interfaces can also be used in type hinting.

An INTERFACE is provided so you can describe a set of functions and then hide the final implementation of those functions in an implementing class. This allows you to change the IMPLEMENTATION of those functions without changing how you use it.

An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behavior.

  • Interfaces are 100% abstract classes – they have methods but the methods have no ‘guts’.
  • Interfaces cannot be instantiated – they are a construct in OOPs that allows you to inject ‘qualities’ into classes .. like abstract classes.
  • Where an abstract class can have both empty and working/concrete methods, interface methods must all be shells – that is to say, it must be left to the class (using the interface) to flesh out the methods.
  • Interfaces cannot contain variables and concrete methods except constants.
  • For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.

 Why use Interface:

  • Interfaces allow you to define/create a common structure for your classes – to set a standard for objects.
  • Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources.
  • Interfaces provide a flexible base/root structure that you don’t get with classes.
  • Interfaces are great when you have multiple coders working on a project – you can set up a loose structure for programmers to follow and let them worry about the details.

 Example 1:

< ?

interface employee

{

function setdata($empname,$empage);

function outputData();

}

class Payment implements employee

{

function setdata($empname,$empage)

{

//Functionality

}

function outputData()

{

echo “Inside Payment Class”;

}

}

$a = new Payment();

$a->outputData();

?>

Example 2:

<?php

// No company just has “employees”, all employees have a particular function or belong to a group.

// So, we don’t want employee to be a concrete (one that can be instantiated) class. Lets create an abstract.

 

abstract class Employee

{

protected $employeeID=”;

protected $annualSalary=NULL;

abstract protected function setEmployeeID();

abstract protected function setAnnualSalary();

 

public function getEmployeeID()

{

return $this->employeeID;

}

 

public function getAnnualSalary()

{

return $this->annualSalaryID;

}

 

}

 

//Now lets define some interfaces based on the description above

//canHackTheSystem and canTakeLongVacations are the adjectives

//they can be used to group employees in a manner that is different then just their job

 

interface canHackTheSystem

{

public function getSecurityClearance();

}

 

interface canTakeLongVacations

{

public function getHRApproval();

}

 

 

// and now, finally our classes. Notice the implements keyword to use interfaces

 

class Engineer extends Employee implements canHackTheSystem

{

protected function setEmployeeID()

{

//read from db, etc.

}

protected function setAnnualSalary()

{

//beg HR for a raise

}

//required by the interface ‘canHackTheSystem’

public function getSecurityClearance()

{

//of course, they have all access

}

}

 

class Executive extends Employee implements canTakeLongVacations

{

protected function setEmployeeID()

{

//read from super-special db, etc.

}

protected function setAnnualSalary()

{

//definitely a large number

}

//required by the interface ‘canTakeLongVacations’

public function getHRApproval()

{

//all those bribes and gifts paid off

}

}

 

//instantiate

$engineer = new Engineer;

$executive = new Executive;

 

class SeniorEngineer extends Engineer implements canHackTheSystem, canTakeLongVacations

{

//she can do it all

}

?>

Share This Post

Post Comment