PHP : OOP - Abstract Classes

In PHP, an abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes and may contain abstract methods that must be implemented by its subclasses. Abstract classes are declared using the abstract keyword.

Abstract Class Syntax:


abstract class AbstractClass {
    // Abstract method declaration
    abstract public function abstractMethod();
    
    // Regular method
    public function regularMethod() {
        // Code
    }
}

 

Abstract Methods:

  • Abstract methods are declared without any implementation.
  • They provide a way to define a method's signature in the abstract class without specifying its implementation.
  • Subclasses of an abstract class must implement all of its abstract methods.

Example of an Abstract Class:

Here's an example to illustrate how abstract classes work in PHP:


abstract class Shape {
    protected $name;

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

    // Abstract method
    abstract public function getArea();
}

class Circle extends Shape {
    private $radius;

    public function __construct($name, $radius) {
        parent::__construct($name);
        $this->radius = $radius;
    }

    public function getArea() {
        return pi() * $this->radius * $this->radius;
    }
}

class Rectangle extends Shape {
    private $width;
    private $height;

    public function __construct($name, $width, $height) {
        parent::__construct($name);
        $this->width = $width;
        $this->height = $height;
    }

    public function getArea() {
        return $this->width * $this->height;
    }
}

// Cannot instantiate an abstract class
// $shape = new Shape(); // Error: Cannot instantiate abstract class Shape

// Creating objects of the concrete classes
$circle = new Circle("Circle", 5);
$rectangle = new Rectangle("Rectangle", 4, 6);

// Calling getArea() on the objects
echo "Area of {$circle->name}: {$circle->getArea()}<br>";       // Output: Area of Circle: 78.53975
echo "Area of {$rectangle->name}: {$rectangle->getArea()}<br>"; // Output: Area of Rectangle: 24

 

In this example:

  • Shape is an abstract class with an abstract method getArea().
  • Circle and Rectangle are concrete subclasses of Shape.
  • Both Circle and Rectangle implement the getArea() method as required by the abstract class.

Points to Note:

  • Abstract classes cannot be instantiated. They are meant to be extended.
  • If a class contains at least one abstract method, the class itself must be declared as abstract.
  • Abstract methods must be implemented by concrete subclasses (unless they are also abstract).

When to Use Abstract Classes:

  • Use an abstract class when you want to provide a common base implementation to all derived classes.
  • Use abstract methods to define a contract that derived classes must follow.

Conclusion:

Abstract classes in PHP are classes that cannot be instantiated and are used as a blueprint for other classes. They can contain both abstract and non-abstract methods, but they must be extended by concrete subclasses that provide implementations for the abstract methods. Abstract classes are useful for defining a common interface and behavior that subclasses must adhere to.