Faysal Ahmed
Oceanize Lab Geeks
Published in
4 min readMay 8, 2018

--

Interfaces vs Abstract Classes

Implementation of PHP Abstract Class & Interfaces

Abstract Class:

An abstract class is a class that contains at least one abstract method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as “abstract”.

The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.

<?phpabstract class AbstractClass
{
// abtract method defined
abstract protected function name();
abstract protected function age();
abstract protected function profession($sex);
// In abstract Class we can also define common method also.
public function getName(){
return $this->name();
}
}
class ManClass extends AbstractClass
{
protected function name() {
return "Faysal Ahmed";
}
public function age() {
return 35;
}
public function profession($sex){
switch ($sex) {
case 'male':
$profession = "Engineer";
break;
case 'female':
$profession = "Doctor";
break;
default:
$profession = "Teacher";
break;
}
return $profession;
}
}
class WomanClass extends AbstractClass
{
protected function name() {
return "Amie Jackson";
}
public function age() {
return 25;
}
public function profession($sex){
switch ($sex) {
case 'male':
$profession = "Engineer";
break;
case 'female':
$profession = "Doctor";
break;
default:
$profession = "Teacher";
break;
}
return $profession;
}
}
$manClass = new ManClass();
$manName = $manClass->getName();
$manAge = $manClass->age();
$manProfession = $manClass->profession("male");
echo "{$manName} is a {$manAge} years old & an {$manProfession}</br>";$womanClass = new WomanClass();
$womanName = $womanClass->getName();
$womanAge = $womanClass->age();
$womanProfession = $womanClass->profession("female");
echo "{$womanName} is a {$womanAge} years old & an {$womanProfession}";

From above code it will output ,
Faysal Ahmed is a 35 years old & an Engineer
Amie Jackson is a 25 years old & an Doctor
So in abstract class we must have to define at least one abstract method inside abstract class. And we can also define common method inside abstract class.

PHP Interfaces:

In PHP, the interface blocks which declares set of functions to be defined with a class to implement this interface. A class can extend more than one interface, thereby, we can simulate multiple inheritances in PHP. Interfaces are defined in the same way as a class, but with the interface keyword replacing the class keyword and without any of the methods having their contents defined. To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.

<?php// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
// Implement the interface
// This will work
class Template implements iTemplate
{
private $vars = array();

public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}

public function getHtml($template)
{
foreach($this->vars as $name => $value) {
$template = str_replace('{' . $name . '}', $value, $template);
}

return $template;
}
}
// This will not work
// Fatal error: Class BadTemplate contains 1 abstract methods
// and must therefore be declared abstract (iTemplate::getHtml)
class BadTemplate implements iTemplate
{
private $vars = array();

public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
}

Interfaces resemble abstract classes in that they include abstract methods that the programmer must define in the classes that inherit from the interface. In this way, interfaces contribute to code organization because they commit the child classes to abstract methods that they should implement. The use of interfaces becomes very helpful when we work in a team of programmers and want to ensure that all the programmers write the methods that they should work on, or even in the case of a single programmer that wants to commit himself to write certain methods in the child classes.

We can implement a number of interfaces in the same class, and so circumvent the law that prohibits the inheritance from more than one parent class. In order to demonstrate multiple inheritance from different interfaces, we create another interface, Vehicle, that commits the classes that implement it to a boolean $hasWheels property.

interface Vehicle {
public function setHasWheels($bool);
public function getHasWheels();
}
class miniCar implements Car, Vehicle {
private $model;
private $hasWheels;

public function setModel($name)
{
$this -> model = $name;
}

public function getModel()
{
return $this -> model;
}

public function setHasWheels($bool)
{
$this -> hasWheels = $bool;
}

public function getHasWheels()
{
return ($this -> hasWheels)? "has wheels" : "no wheels";
}
}

Key point of interfaces:

  • Interfaces can include abstract methods and constants, but cannot contain concrete methods and variables.
  • All the methods in the interface must be in the public visibility scope.
  • A class can implement more than one interface, while it can inherit from only one abstract class.

--

--