Abstract Classes in PHP

Last updated on August 9, 2023

Abstract classes are a powerful concept in object-oriented programming. They are used to define abstract representational and behavioral combinations, which means they cannot be instantiated or created without being subclassed.

Moreover, abstract classes have one abstract method, which cannot have any code added to it. Furthermore, abstract methods must consistently be implemented differently in child classes.

Syntax

abstract class ClassName {
    abstract protected function methodName();
}

The syntax for defining an abstract class in PHP is similar to that of a regular class, with the exception that the abstract keyword is used before the class keyword.

Advantages of Abstract Classes

There are some following benefits to using abstract classes; let me lift the curtain with these;

  • Abstract classes promote code reusability, as developers have to write a lesser amount of code, resulting in increased development efficiency.
  • Abstract classes provide developers with a way to encapsulate common properties and methods. By doing so, abstract classes make it easier to maintain consistency among related classes.
  • In addition to code reusability, abstract classes can also improve the design of a code base. This can lead to more organized and efficient code, as well as easier maintenance in the long run.
  • Moreover, abstract classes offer a degree of flexibility that can be incredibly valuable in certain situations and developers can easily create customized for specific purposes.

How to Use Abstract Classes in PHP?

By using an abstract class, we can define a set of methods that must be executed by its subclasses, while also providing a default implementation for some methods. Following that, it allows us to create a hierarchy of classes that share some of the quite rife functionality, but can also have their own unique behavior. Actually, abstract classes are a way to define a base class in PHP that cannot be instantiated directly but can be subclassed by other classes easily.

Moreover, abstract classes can comprise both abstract as well as non-abstract methods, and they can also have astonishing properties. In a nutshell, it’s simple to use, for this you have to create an abstract class in PHP by using the “abstract “keyword before the class declaration.

Difference between Abstract Classes and Interfaces

They are both designed to be inherited by other classes and define a set of common properties and methods that their subclasses can use.  An abstract class can have protected or private properties, while an interface can only have public properties. When deciding whether to use an abstract class or an interface, you should consider the specific needs of your code and choose the one that best fits your requirements.

Abstract ClassInterface
It allows to add propertiesYou can’t add properties
Public and protected methods are allowedOnly public methods are allowed
It can have a constructorCan’t have a constructor
table for difference between abstract class and interface in PHP

Example #1 of an Abstract Class in PHP

Let’s take an example to understand the concept of the abstract class better. In the code snippet below, you can see that both Box class and dimension() method are declared as abstract. Here simply, it means that any class that extends the Box class must implement the dimension() method.

abstract class Box {
    abstract protected function dimension();
}

In this example, the ToolBox class extends the Box class and implements the dimension() method. The dimension() method in the ToolBox class calculates the dimension of a ToolBox based on its height and width.

class ToolBox extends Box{
    protected $height;
    protected $width;
  
    public function __construct($height,$width) {
        $this->height = $height;
        $this->width = $width;
    }
  
    protected function dimension() {
        return $this->width * $this->height;
    }
}

By implementing the dimension() method, the ToolBox class becomes a concrete class and can be instantiated.

Example #2 of an Abstract Class in PHP

In this example, we’ll create an abstract class called Vehicle and then extend it with two concrete subclasses: Car and Motorcycle.

// Abstract class
abstract class Vehicle {
    protected $brand;

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

    abstract public function start();
    abstract public function stop();

    public function getBrand() {
        return $this->brand;
    }
}

// Concrete subclass: Car
class Car extends Vehicle {
    public function start() {
        return "Starting the car's engine.";
    }

    public function stop() {
        return "Stopping the car.";
    }
}

// Concrete subclass: Motorcycle
class Motorcycle extends Vehicle {
    public function start() {
        return "Kicking off the motorcycle.";
    }

    public function stop() {
        return "Parking the motorcycle.";
    }
}

// Create instances of subclasses
$car = new Car("Toyota");
$motorcycle = new Motorcycle("Honda");

Use methods from the abstract class and its subclasses

echo $car->getBrand() . ": " . $car->start() . " " . $car->stop();
echo $motorcycle->getBrand() . ": " . $motorcycle->start() . " " . $motorcycle->stop();

In this example, we defined an abstract class Vehicle with a constructor that sets the brand property. It also includes two abstract methods: start() and stop().

After that, we created two concrete subclasses, Car and Motorcycle, both of which extend the Vehicle abstract class.

Each concrete subclass provides its own implementation of the start() and stop() methods, fulfilling the contract imposed by the abstract class.

In the end, we created instances of the Car and Motorcycle subclasses and demonstrate how to call methods from the abstract class and its subclasses.

Conclusion

In the final verdict, we can say abstract classes in PHP are classes that cannot be instantiated on their own but can only be extended by other classes. Moreover, abstract classes have one abstract method, which cannot have any code added to it. The example of the Box class and its dimension() method helps illustrate the concept of abstract classes and their usage in PHP programming.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: PHP, PHP-OOP,