php
  1. php-abstraction

PHP Abstraction

Abstraction in PHP is the concept of hiding the implementation details of a certain functionality and only showing the essential features to the user. This helps in reducing complexity and enhances the usability of the code.

Syntax

To implement abstraction in PHP, we can use the abstract keyword along with the class keyword.

abstract class Example {
    // abstract methods
    abstract function calculate();
    abstract function display();
}

Example

Let's consider an example where we create an abstract class called Shape which has an abstract method calculateArea(). This class can be inherited by multiple shape classes like Triangle, Square, and Circle.

// Abstract class
abstract class Shape {
    // Abstract Method
    abstract public function calculateArea();
}

// Child classes
class Triangle extends Shape {
   private $base = 0;
   private $height = 0;

   public function __construct($base, $height) {
      $this->base = $base;
      $this->height = $height;
   }

   // Calculate area for triangle
   public function calculateArea() {
      return 0.5 * ($this->base * $this->height);
   }
}

class Square extends Shape {
   private $length = 0;

   public function __construct($length) {
      $this->length = $length;
   }

   // Calculate area for square
   public function calculateArea() {
      return ($this->length * $this->length);
   }
}

class Circle extends Shape {
   private $radius = 0;

   public function __construct($radius) {
      $this->radius = $radius;
   }

   // Calculate area for circle
   public function calculateArea() {
      return (3.14 * ($this->radius * $this->radius));
   }
}

// Initialize Shape objects
$triangle = new Triangle(10, 20);
$square = new Square(15);
$circle = new Circle(7);

// Display the areas of the shapes
echo "Area of Triangle: " . $triangle->calculateArea() . "<br/>";
echo "Area of Square: " . $square->calculateArea() . "<br/>";
echo "Area of Circle: " . $circle->calculateArea() . "<br/>";

Output

Area of Triangle: 100
Area of Square: 225
Area of Circle: 153.86

Explanation

In the above example, we have created an abstract class Shape with an abstract method calculateArea(). This class acts as a blueprint for all the child classes like Triangle, Square, and Circle.

Each child class inherits the calculateArea() method from the parent class Shape and implements it according to their particular shape's formula.

We have created objects of each child class and called their respective calculateArea() methods to calculate the area of each shape.

Use

Abstraction is an important aspect of object-oriented programming as it helps in reducing complexity and increasing the reusability of the code.

Important Points

  • Abstraction is achieved in PHP using the abstract keyword along with the class keyword.
  • An abstract class cannot be instantiated and can only be used as a base class.
  • Abstract methods do not have any implementation and must be implemented by the child classes.
  • A child class must implement all the abstract methods defined in its parent abstract class.

Summary

Abstraction is an important concept in object-oriented programming that helps in hiding the implementation details of a certain functionality and only showing the essential features to the user. It is achieved in PHP using the abstract keyword along with the class keyword. An abstract class cannot be instantiated and can only be used as a base class. Abstract methods must be implemented by the child classes.

Published on: