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.


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


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):

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";
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 passionate about technology and building products. A web design business created while attending college slowly evolved into a brick and mortar on Main St. of his small Midwestern hometown. His desire for fame and fortune led to the co-founding of a Y Combinator startup and a life in California.


Hey there! I'm currently writing a book on Microservices which I expect to release in early 2017. If you're interested in getting updates please signup here. More info about the Book
  • 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.