PHP abstract class and function – Abstraction in PHP

Spread the love

PHP abstract class and function – Abstraction in PHP

PHP abstract class and function: In this chapter, we will see another object oriented programming feature that is an abstraction.

The literal meaning of a word abstract is “existing in thought or as an idea but not having a physical or concrete existence”.

It means abstraction is just a concept.

In programming, abstraction is just a declaration of functionality but not a definition at the initial level.

Let’s see the above description in detail.

In the previous chapter, you have learned how we can use inheritance or a parent-child relationship in between classes for code reuse.

Or when there is a duplicate code occurs in different classes then we grouped that duplicate code into a parent class. Then that parent class will be extended by different child classes and use the code present in a parent class.

So in this case parent class would have common methods with same functionality applicable to every child class.

But there are certain conditions where the method name of parent class used by the child classes needs to be same but the functionality of that method will differ as per the child class or you can say that the definition of methods is incomplete.

These incomplete methods are not having a method body in a parent class, but just a declaration.

So such incomplete or just defined methods are called abstract methods and they can be declared by placing a keyword abstract in front of them, as shown here.

abstract public function hello();

For defining an abstract class lets see an example.

PHP abstract class and function example:

Different shapes like square, rectangle, circle are all called as shapes.

So we can group them in a parent class Shape. These shapes are having few properties and functionalities in common like length, breadth, area, etc.

But the calculation of area for each shape is different. So we cannot keep the method with the same functionality, which calculates the area for each shape in parent Shape class.

But still, we want that each shape MUST have a method to calculate an area of a shape.

Then, in this case, we declare an abstract method named say “showArea” in a parent Shape class and implement it in every child class with different functionality as per the child shape class.

<?php 
class Shape
{
 abstract public function showArea();
}
class Square extends Shape
{
 private $length;
 
 public function __construct($length)
 {
  $this->length = $length;
 }
 
}

class Rectangle extends Shape
{
 private $length;
 private $breadth;
 
 public function __construct($len, $brth)
 {
  $this->length = $len;
  $this->breadth = $brth;
 }
 
}
$sqr = new Square(10);
echo "Area of a square = ".$sqr->showArea();

$rect = new Rectangle(10,20);
echo "<br>Area of a square = ".$rect->showArea();
?>

As you see in above program we have declared parent class “Shape” having an abstract method declaration “showArea”. 

And child classes Square and Rectangle extended the Shape class. But they have not yet implemented the “showArea” method.

After running the above program you will get a Fatal error as:

“Fatal error: Class Shape contains 1 abstract method and must, therefore, be declared abstract or implement the remaining methods (Shape::showArea)…..“.

You will see this kind of Fatal error because of certain rules of abstract class and abstract methods.

These rules say:

  1. Every abstract method declared in a parent class should be implemented in a child class. It means every abstract method should have a definition or a method body in the child class.
  2. Every class which is having at least one abstract method should also be declared as an abstract class. That is you need to put the “abstract” keyword before a “class” keyword.
  3. If the child class not implementing an abstract class, then that child should also be declared as an abstract class.

We have missed all the above rules in our program. So let’s update our program as per the rules.

Updates will be:

  1. Add “abstract” keyword before a parent class Shape like – abstract class Shape{ }.
  2. Implement “showArea” abstract method in child classes “Square” and “Rectangle”.

Hence the updated program will be.

<?php 
abstract class Shape
{
 abstract public function showArea();
}
class Square extends Shape
{
 private $length;
 
 public function __construct($length)
 {
  $this->length = $length;
 }
 public function showArea()
 {
  $area = $this->length * $this->length;
  return $area;
 }
 
}

class Rectangle extends Shape
{
 private $length;
 private $breadth;
 
 public function __construct($len, $brth)
 {
  $this->length = $len;
  $this->breadth = $brth;
 }
 
 public function showArea()
 {
  $area = $this->length * $this->breadth;
  return $area;
 }
 
 
}
$sqr = new Square(10);
echo "Area of a square = ".$sqr->showArea();

$rect = new Rectangle(10,20);
echo "<br>Area of a square = ".$rect->showArea();
?>

In the above program, we have implemented “showArea” abstract method with a different definition for each child class, because each shape is having a different process of area calculation.

The compulsion of implementing abstract functions into child classes is enforcement that the child classes should have these methods for the working.

So that you can force any class to include those functions or methods in it, which are declared as an abstract in the parent class.

I hope this tutorial about PHP abstract class and function explains you about the abstraction in PHP.

In the next chapter, you will learn what is Interface and its use in detail.


Spread the love

Leave a Comment