Posted by & filed under PHP.

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++).

Note: For a more in-depth look at Object Oriented Programming in PHP, checkout Modern PHP: New Features and Good Practices. It contains much more information than is included in this blog post.

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”.

Thomas Hunter II

Thomas is the author of Advanced Microservices and a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.

Latest posts by Thomas Hunter II (see all)

Tags:

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

  • Pingback: Zend Certified Engineer (ZCE) Study Guide Links()

  • 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.

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

  • Pingback: Zend Certified Engineer (ZCE) Study Guide Links « Renowned Media()

  • Nadeem

    Is this really polymorphism?

  • faiz

    Really helped me getting the idea of abstract classes and interfaces. Thanks

  • Fazal

    Where is Polymorphism in this example. All is basic Inheritance. Polymorphism let you create Object at runtime.

  • Fazal, this is an example of polymorphism. Method overriding is a way to achieve polymorphic behavior. There can be static and dynamic versions of polymorphism, and this shows more of a static version. It seems like you are thinking of a dynamic version maybe? Interesting example Thomas! It would be cool to hear your thoughts on my explanation of polymorphism with PHP over at my site.