Abstract Classes in PHP

Last updated on March 24, 2023 by Ayaz Ali Shah

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.

Example 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.

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.


Related Posts

Tags: PHP, PHP-OOP,