Home About Contact DOTW RSS

Get Social with GeekZenith...

OOP in PHP Part Two: Inheritance

Object-Oriented Programming in PHP

Inheritance

The second part in my series of OOP in PHP tutorials will focus on OOP Inheritance. In this tutorial I will explain what OOP Inheritance is, how to use it and what benefits it can provide using a real-world and entirely understand example to illustrate my words.

Before we continue, this tutorial is written with the assumption that you have a basic knowledge of PHP and have read my previous tutorial – Basic OOP in PHP Part One.

Inheritance Explained

OOP Inheritance basically allows you to build upon existing classes and in coding terms these are called extends. The idea is to maintain the integrity of the original class so that its function remains the same, but share its fields and methods with an additional class in order to develop existing functionality.

We’ll take the VAT Calculation function we created in the last OOP in PHP tutorial to demonstrate the functionality of PHP extends. If you missed it, we created two PHP pages – one called index.php and the other class_lib.php. The class_lib.php file stored all of our OOP functions and index.php served as our human interface file that not only displayed the results of our function, but contained the user-defined data that feed the function. The files can be downloaded here, but when we were last finished with them, they were set up as below:

class_lib.php:


vat = ($figure * 1.175) - $figure;
	}

	function GetVat() {
	return $this->vat;
	}

}
?>

And index.php:


CalcVat("500");
echo 'You would have to pay: £'.$vatPrice->GetVat().' in VAT. ';
?>

The code above took the user-defined figure, calculated the VAT, subtracted the initial figure and brought back only the amount of VAT that should be paid on that figure. We are going to use an extend and create a method to calculate the VAT and return the total price (the user-defined figure and the VAT together). To do this, we’ll use the VAT class $vat field and the GetVAT method to return the value. In order to perform the altered calculation, we’re going to set up an extended class and call it TotalVat.

The syntax of the class extension is always the same:


class NEWCLASSNAME extends OLDCLASSNAME  {

}

So in our case this will be:


class TotalVAT extends VAT {

}

Since we’re using the existing classes field ($vat), we don’t need to re-define it in this new class – nor do we have to re-defined the GetVAT() method we use to return the final value. All we need to do is to set up the method that will calculate the VAT differently – we’ll call this TotVAT and it’ll look like this:


class TotalVAT extends VAT {

	function TotVAT($figure)
	{
	$this->vat = $figure * 1.175;
	}

}

Making the entire class_lib.php page like this:


vat = ($figure * 1.175) - $figure;
	}

	function GetVat() {
	return $this->vat;
	}

}

class TotalVAT extends VAT {

	function TotVAT($figure)
	{
	$this->vat = $figure * 1.175;
	}

}

?>

We call to this new class extension as we would any other. Let me remind you of the syntax we used in calling to the original class in the last tutorial:


$vatPrice = new VAT();
$vatPrice->CalcVat("500");
echo 'You would have to pay: £'.$vatPrice->GetVat().' in VAT. ';

To call our extend class, very little changes; instead of VAT(); we're calling to TotalVAT() and instead of CalcVat() we're calling to totVAT(). However, we'll still be calling to the same GetVat() method to return the end value.


$totVat = new TotalVAT();
$totVat->totVAT("500");
echo 'You would have to pay: £'.$totVat->GetVat().' in Total VAT.';

With the value of 500 entered, this extension returns the total value of £582.50.

The entire index.php now looks like:


CalcVat("500");
echo 'You would have to pay: £'.$vatPrice->GetVat().' in VAT. ';

$totVat = new TotalVAT();
$totVat->totVAT("500");
echo 'You would have to pay: £'.$totVat->GetVat().' in Total VAT.';
?>

Click here to download the files for this tutorial.
Any questions regarding OOP Extends? If so, comment below or hit our contact page and submit your query.

Like this fantastical post on Facebook!

Share this awesometastic post with your friends on Facebook!

Leave a Reply