PHP Abstract Classes

This tutorial will help you learn abstract classes and methods.

 

What is PHP Abstract Classes?

PHP Abstract Classes are the classes where at least one method is abstract. You can declare an abstract class using the abstract keyword. However, you cannot instantiate a class that is defined as abstract.


A few important points about abstract class and method are as follows:

  • It can have methods and properties similar to any other normal class.
  • It cannot be instantiated; therefore, you are required to create a child class that extends it; after that, you can create an object of the child class.
  • If there is even a single abstract method in the class, then the class should also be abstract.
  • The abstract method is simply the declaration, where the method's name and argument are provided, while the body part is empty.

All the points will be covered step by step with examples. Let us begin by understanding how to create an abstract class.

 

 

Creating an abstract Class

You are required to use the abstract keyword before the name of the class for declaring a class abstract.

Let's take an example:

<?php
    // abstract class
    abstract class Vehicle {
        
        // abstract function mileage
        abstract public function mileage() {
            
        }
    }
    
?>

 

In the above-mentioned example, the class Vehicle is an abstract class, having an abstract method. 

The purpose behind creating an abstract class is to bind developers to follow a set of guidelines. For example, for creating a new class that extends our class Vehicle, you will be required to define the abstract method mileage(), or else the child class should also be abstract. Therefore, all the child classes must define the method mileage().

 

Non Abstract Method in Abstract Class

Although a class with a single abstract method should be declared abstract, an abstract class can also have non-abstract methods, which child classes can access and use directly without overriding them.

Now, let us extend the above-mentioned example and include a non-abstract method in our class Vehicle:

<?php
    // abstract class
    abstract class Vehicle {
    
        // protected variable
        protected $name;
    
        // non-abstract public function start
        public function start() {
            echo $this->name. " - Engine start...<br/>";
        }
        
        // non-abstract public function stop
        public function stop() {
            echo $this->name. " - Engine stop...<br/>";
        }
        
        // non-abstract public function setName
        public function setName($name) {
            $this->name = $name;
        }
        
        // abstract function mileage
        abstract public function mileage() {
            
        }
    }
    
?>

In the above-mentioned code, three non-abstract methods namely start(), stop() and setName() have been added to our abstract Vehicle class.

 

Inheriting Abstract Classes

You can also create classes extending abstract classes too.

Here the child class must define the abstract method that has been declared in the abstract parent class.

If the child class does not define the abstract method, it must be defined as an abstract class.

Let us create two child classes that inherit the class Vehicle and which will have a definition for the abstract method mileage():

 

<?php
    // child class
    class Car extends Vehicle {
        
        public function mileage() {
            echo "I am " . $this->name . "<br/>";
            echo "My mileage range is - 15 to 22 Km/L";
        }
        
    }
    
?>

You can have as many child classes as you want; let us have another class:

<?php
    
    // child class
    class Motorcycle extends Vehicle {
        
        public function mileage() {
            echo "I am " . $this->name . "<br/>";
            echo "My mileage range is - 35 to 47 Km/L";
        }
        
    }
    
?>

 

As we mentioned above, an abstract class cannot have any object, so once we have a proper child class defined, we can create an object for them.

<?php

    $car = new Car();
    $car->setName("BMW X1");
    $car->mileage();
    
?>

Output:

I am BMW X1
My mileage range is - 15 to 22 Km/L

 

You can try to create an object of the class Vehicle, but you will get an error.