In any software application, not all of the logic of the program is shown to the end user. With the help of OOP, the code implementation remains hidden, which is called data abstraction. This tutorial explains the Abstract class and how it relates to the object-oriented approaches of PHP. We will also see how to implement an abstract class with some examples.



What is Abstract Class?

An abstract class in PHP is a class that cannot be instantiated on its own but can be extended by other classes. An abstract class is a blueprint for one or more derived classes. It defines a set of common methods that the derived classes must implement, but it does not provide an implementation for those methods.

An abstract class is a mixture of a class and an interface. It specifies both functionality and user interface. We cannot instantiate an abstract class, and every abstract class with at least one abstract method must be abstract. If we define a Method as abstract, it signifies the Signature of the Method.

Declaring an Abstract Class

To declare an abstract class in PHP, use the abstract keyword before the class keyword. Here is an example of how to declare an abstract class:

Example:

abstract class Shape {

}

Declaring Abstract Methods

An abstract method is a method that is declared in an abstract class but does not have an implementation. Abstract methods are used to define the interface for a class but leave the implementation up to the child class.

To declare an abstract method in PHP, use the abstract keyword before the method signature. Abstract methods must not have a body, as they do not have an implementation.

abstract class Shape{
  // Abstract methods
  abstract public function area();
  abstract public function perimeter();
}

In the example above, the Shape class has two abstract methods, area and perimeter, that must be implemented by any class that extends Shape.

Extending an Abstract Class

To create a new class that extends an abstract class, use the extends keyword. The child class must implement all abstract methods declared in the parent class.

Example:

class Circle extends Shape {
  private $radius;

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

  // Implementing the abstract methods
  public function area() {
    return pi() * pow($this->radius, 2);
  }

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

In the example above, the Circle class extends the Shape class and implements the area and perimeter methods.

Using Abstract Classes

To use an abstract class, you must create an instance of a derived class that implements all the abstract methods of the abstract class. You cannot create an instance of the abstract class itself.

Here is an example of how to use the Circle class we defined above:

$circle = new Circle(5);
echo $circle->area(); // 78.5398163397448
echo $circle->perimeter(); // 31.41592653589793

Abstract Properties

In addition to abstract methods, an abstract class can also have abstract properties. Abstract properties are like abstract methods, but do not have a body and are declared using the var keyword.

Here is an example of an abstract class with an abstract property:

Example:

abstract class Shape {
  // Abstract property
  abstract var $color;

  // Abstract methods
  abstract public function area();
  abstract public function perimeter();
}

The derived class must define the value of the abstract property. Here is an example of a derived class that defines the value of the color property:

class Circle extends Shape {
  private $radius;

  // Defining the value of the abstract property
  var $color = 'red';

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

  // Implementing the abstract methods
  public function area() {
    return pi() * pow($this->radius, 2);
  }

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

Conclusion

Abstract classes are a helpful tool in object-oriented programming, as they allow you to define an interface for a class and leave the implementation up to the child classes. Remember that abstract classes cannot be instantiated directly and that child classes must implement all abstract methods declared in the parent class.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram