PHP Abstract Class: Syntax, Examples & Usage

1. Introduction

In Object-Oriented Programming (OOP), abstraction is an important concept that helps developers build clean and organized code. Abstraction allows programmers to hide unnecessary implementation details and expose only the required functionality to the user.

In PHP, abstraction is mainly achieved using Abstract Classes and Interfaces. An Abstract Class provides a base structure for other classes and ensures that certain methods must be implemented in child classes.

πŸ“–
Key characteristics of PHP Abstract Classes:
  • They cannot be instantiated directly.
  • They can contain both abstract methods and normal methods.
  • Child classes must implement all abstract methods.
  • They are mainly used for inheritance and code reuse.

2. What is Abstraction?

Abstraction is the process of hiding implementation details and showing only the essential features of an object.

In simple words, abstraction focuses on what an object does instead of how it does it.

Real-life Example

When you start a car, you simply turn the key or press a button. You do not need to understand the internal mechanism of the engine, fuel system, or electrical system.

You only interact with the functionality of starting the car. The internal complexity is hidden from the user.

This concept is exactly what abstraction does in programming.

πŸ“–
Benefits of abstraction:
  • Reduces code complexity.
  • Improves security by hiding internal details.
  • Makes programs easier to maintain.
  • Encourages code reuse.

3. What is a PHP Abstract Class?

A PHP Abstract Class is a class declared with the abstract keyword.

Abstract classes are special classes that cannot be instantiated directly. Instead, they are designed to be inherited by other classes.

Syntax:


abstract class ClassName {
}
πŸ“–
Important notes about abstract classes:
  • Abstract classes can contain abstract methods and normal methods.
  • At least one abstract method must be declared in an abstract class.
  • Abstract classes are mainly created for inheritance purposes.
  • A child class must implement all abstract methods.

Example declaration:


abstract class Shape {
   abstract public function calculateArea();
}

Here, the class defines a method but does not implement it. The implementation will be done by child classes.

4. Example of Abstract Class and Abstract Method

Let’s understand this concept with a practical example.


abstract class Area {

   abstract public function getArea();

   public function showArea() {
       echo $this->getArea();
   }

}

class Rectangle extends Area {

   public $length = 100;
   public $width = 50;

   public function getArea() {
       return "Rectangle Area is " . ($this->length * $this->width);
   }

}

class Triangle extends Area {

   public $base = 50;
   public $height = 20;

   public function getArea() {
       return "Triangle Area is " . ($this->base * $this->height / 2);
   }

}

$rec_obj = new Rectangle;
$rec_obj->showArea();

echo "
"; $tri_obj = new Triangle; $tri_obj->showArea();

Output

Rectangle Area is 5000 Triangle Area is 500

Explanation

  • The Area class is an abstract class.
  • It contains an abstract method getArea().
  • The Rectangle and Triangle classes extend the abstract class.
  • Both classes implement the getArea() method using their own formulas.

This ensures that every shape class must define how its area is calculated.

5. Cannot Create Object of Abstract Class

One important rule of abstract classes is that you cannot create an object directly from an abstract class.

Example:


abstract class Student {

   public $name = "John";

   public function student_name() {
       return $this->name;
   }

}

$stu_obj = new Student;
echo $stu_obj->student_name();

Output

Fatal error: Cannot instantiate abstract class Student

Explanation:

Since the class is declared as abstract, PHP does not allow object creation from it.

Instead, the class must be extended by another class.

6. Abstract Method

An Abstract Method is a method that is declared but does not contain any implementation.

It must be implemented by the child class.

Syntax:


abstract function functionName();
πŸ“–
Important rules:
  • Abstract methods can only exist inside abstract classes or interfaces.
  • The method must be implemented in the child class.
  • The child class must use the same method signature.

Example:


abstract class Country {
   abstract public function country_name($name);
}

class India extends Country {
   public function country_name($cname) {
       echo "Country name is " . $cname;
   }
}

$in_obj = new India;
$in_obj->country_name("India");

Output:

Country name is India

7. Why Use Abstract Classes and Methods?

Many beginners wonder why abstract classes are necessary.

Let’s understand this with an example.

Without Abstract Class


class Animals {
   public function givemilk() {
       echo "Animals give us milk.";
   }
}

class Cow extends Animals {

   public function givemilk() {
       echo "Cow gives us milk.";
   }

}

class Buffalo extends Animals {

   public function givemilk() {
       echo "Buffalo gives us milk.";
   }

}

$cow_obj = new Cow;
$cow_obj->givemilk();

$buf_obj = new Buffalo;
$buf_obj->givemilk();

$ani_obj = new Animals;
$ani_obj->givemilk();

Output

Cow gives us milk.
Buffalo gives us milk.
Animals give us milk.

Problem:

Not all animals give milk, so the Animals class should not implement this method directly.

With Abstract Class


abstract class Animals {

   abstract public function givemilk();

}

class Cow extends Animals {

   public function givemilk() {
       echo "Cow gives us milk.";
   }

}

class Buffalo extends Animals {

   public function givemilk() {
       echo "Buffalo gives us milk.";
   }

}

$cow_obj = new Cow;
$cow_obj->givemilk();

$buf_obj = new Buffalo;
$buf_obj->givemilk();

Output

Cow gives us milk.
Buffalo gives us milk.

Now the problem is solved because:

  • The Animals class cannot be instantiated.
  • Only specific animals define the method.

9. Conclusion

The PHP Abstract Class is an essential concept in Object-Oriented Programming that helps enforce structure and consistency in code.

Abstract classes allow developers to define a common base class while leaving specific implementations to child classes. They help achieve abstraction, code reuse, and better program design.

PHP Abstract Class – Interview Questions

Q 1: What is an abstract class?
Ans: A class that cannot be instantiated and may contain abstract methods.
Q 2: How do you declare an abstract class?
Ans: Using the abstract keyword.
Q 3: Can abstract classes have non-abstract methods?
Ans: Yes.
Q 4: Must child classes implement abstract methods?
Ans: Yes.
Q 5: Difference between abstract class and interface?
Ans: Abstract classes can have properties and implementations; interfaces cannot.

PHP Abstract Class – Objective Questions (MCQs)

Q1. Abstract classes are declared using ______.






Q2. Abstract methods ______.






Q3. Can an abstract class be instantiated directly?






Q4. Abstract class can have ______.






Q5. Abstract classes are used to ______.






Related PHP Tutorials