Polymorphism, Abstract Classes, and Interfaces in PHP

Introduction

This article will demonstrate Abstract classes, Interfaces, and polymorphism, as implemented in the PHP5 language, which isn’t as versatile as other languages I’ve worked with (such as C++).

Interfaces

An Interface is kind of like a class, although you can’t use it directly, classes must implement them. Think of it as a prototype or a blueprint of a class. In your interface you will declare the functions, but they won’t actually have any code inside of them. You also aren’t allowed to define your data storage.

In the classes that you implement your interface in, you must use all of the defined functions of the interface, otherwise you’ll get a PHP error. This is because our program which will interact with the implemented classes expect our class to be able to communicate in the methods that we define here. This way, as we modify functionality elsewhere in the application, or add extra functionality in our implementing classes, we don’t break backwards compatibility.

Abstract Classes

An abstract class is like a normal class, except that you cannot instantiate them, you must extend them first then instantiate. Your abstract classes can have data members and function calls (and their functionality).

Polymorphism

When we start to extend classes, and add functionality to them which wasn’t there previously, and even override existing methods (functions), this is called polymorphism. Here’s my example of this (not the best but it shows the basics):

<?php
interface Human {
 public function getName();
 public function setName($name);
}

abstract class Military {
 private $rank;

 public function __construct($rank) {
 $this->rank = $rank;
 }
 public function setRank($rank) {
 $this->rank = $rank;
 }
 public function getRank() {
 return $this->rank;
 }
}

class Soldier extends Military implements Human {
 private $name;

 public function __construct($name, $rank) {
 $this->name = $name;
 parent::__construct($rank); # parent::setName($rank);
 }
 public function setName($name) {
 $this->name = $name;
 }
 public function getName() {
 return "My name is: " . $this->name . "<br />n";
 }
 public function getRank() {
 return "My rank is: " . parent::getRank() . "<br />n";;
 }
 public function getFull() {
 return "I am " . parent::getRank() . " {$this->name}<br />n";
 }
}

$goodSoldier = New Soldier('Thomas', 'Officer');

echo $goodSoldier->getName();
echo $goodSoldier->getRank();
echo $goodSoldier->getFull();
echo "<br />n";
$goodSoldier->setRank('Colonel');
$goodSoldier->setName('Mustard');
echo $goodSoldier->getName();
echo $goodSoldier->getRank();
echo $goodSoldier->getFull();

The output of our code is the following:

My name is: Thomas
My rank is: Officer
I am Officer Thomas

My name is: Mustard
My rank is: Colonel
I am Colonel Mustard

Also, funny trivia, the double colon character (::) is referred to by its Hebrew name, “Paamayim Nekudotayim”.

7 Responses to Polymorphism, Abstract Classes, and Interfaces in PHP
  1. Aaron Godin Reply

    Hey thanks a lot for this article. It’s a great help to me in understanding these object-oriented approaches in PHP.

  2. Jurgen Reply

    This tutorial is quite alright. The only thing is that i wouldn’t extend my human class to military sinds a human is not a military or a military is not a human, you’d rather call the class Soldier. The rule i’ve learned is that you only extend if something is equal to the other. example:

    class animal

    class cat

    class cat extends animal

    Because a cat is an animal you’d extend the class.

    • Thomas Hunter Reply

      Obviously you wouldn’t want to take this example and run with it for your military personnel website.

  3. Nadeem Reply

    Is this really polymorphism?

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>