Understanding Polymorphism As Wrapper Class With PHP Example

Introduction

In programming languages, polymorphism means that some code or operations or objects behave differently in different contexts.
It able to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.
So it is overloading parent object. Child objects has all methods in their parent and childs can have more methods.

Several parent object type to support polymorphism are class, abstract and interface. In this post, I use ‘abstract’ class as parent to give polymorphism demo due to I will using it as wrapper class.
What I mean of wrapper class is I do not have to write same code over and over again in every child classes. I only have to write once in the abstract functions.
Abstract class can not have an instance or real object. It is only model of the class design but the different from ‘interface’ that abstract can have a body inside function declaration.

To explain the differences between abstract, interface and general class, look at code below:

abstract class AAA {
	function set_varA() {
		echo "set AAA<br>"; // abstract can have body inside function
	}
}

interface iBBB {
	function set_varB(); // interface can not have body inside function but declaration only
}

$abs = new abstract(); // this will give execution error because of abstract is design model only and can not have instance

class CCC {
	function set_varC() {
		echo "Set CCC<br>"; // regular class can set body inside function
	}
}

$c = new class CCC(); // regular class can have instance

Example

Here’s the PHP code example to demonstrate Polymorphism using abstract. That abstract class is a wrapper of method(s).

abstract class employee {
	function set_meal_allowance() {
		echo "set meal allowance<br>";
	}
}

class manager extends employee {
	function set_transport_allowance() {
		echo "set transport allowance<br>";
	}
}

class staff extends employee {
	function set_manager() {
		echo "set manager<br>";
	}
}

$mng = new manager();
$mng->set_meal_allowance();
$mng->set_transport_allowance();

$stf = new staff();
$stf->set_meal_allowance();
$stf->set_manager();

We have employee abstract class. This employee can not have instance because of it is an abstract. Let’s see employee case in real world.
If we talk about employee then it is too general. I mean, what kind of employee he/she is? Is he/she a director, manager, staff or just temporary contract employee or else?
So I do not set employee to have an instance or real object. That’s why I set employee to abstract class.

Employee has set_meal_allowance() method. Every child class that inherit employee will have set_meal_allowance() method. Manager & staff has it but manager has set_transport_allowance().
Same as staff and it has set_manager(). The parent class does not have access to child classes methods.

In child classes I do not have to write set_meal_allowance() to have a function because I already inherit from employee abstract. That’s why the abstract in this class called wrapper sometimes.
Exceptional can occur if you want to override the set_meal_allowance() function at child classes, you have to write set_meal_allowance() in child class and related function body.

Implementation lines shows manager & staff accessing parent’s and their specific methods.

Regards,
Agung Gugiaji

Advertisements

One response to “Understanding Polymorphism As Wrapper Class With PHP Example

  1. K.I.S.S great explanation in plain simple terms thank you …

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s