Abstraction in PHP (Abstract Classes & Methods)

Introduction

Abstraction is one of the most important concepts in Object-Oriented Programming (OOP) because it helps developers focus on essential functionality while hiding unnecessary implementation details.

In real-world applications, we often want to define a common structure that multiple classes must follow, but we do not want the base class to be used directly. This is where abstract classes become useful.

An abstract class acts as a blueprint for other classes. It defines what a class must do, while child classes decide how that work should be performed.

This helps create cleaner, more maintainable, and scalable PHP applications.

In this tutorial, you will learn what abstraction is, how abstract classes work in PHP, and how to use them with simple real-world examples.

What is Abstraction?

Abstraction means hiding implementation details and showing only what is necessary.

In simple words:

You define what a class must do, not how it will do it.

Abstraction focuses on design, not implementation.

How Abstraction is Achieved in PHP?

In PHP, abstraction is implemented using:

  • Abstract classes
  • Abstract methods

What is an Abstract Class?

An abstract class:

  • Cannot be instantiated (you cannot create its object)
  • Can contain abstract and non-abstract methods
  • Can contain properties
  • Is meant to be extended by child classes

In PHP, we use the abstract keyword to declare an abstract class.

Abstract Class Syntax

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

Example: Abstract Class in PHP

<?php
abstract class Payment {
    abstract public function pay($amount);

    public function paymentInfo() {
        echo "Processing payment...";
    }
}
?>

In this example:

  • Payment is an abstract class.
  • pay() is an abstract method (no body).
  • paymentInfo() is a normal method.

What is an Abstract Method?

An abstract method:

  • Is declared using the abstract keyword
  • Does not contain a body
  • Must be implemented in the child class

Syntax:

abstract public function methodName();

Notice that there is no {} body.

Abstract Method Rules

  • Must be declared using abstract keyword
  • Must not have a body
  • Child class must implement it

Implementing Abstract Class

Now let’s create child classes:

<?php
class CardPayment extends Payment {
    public function pay($amount) {
        echo "Paid ₹$amount using Card";
    }
}

$payment1 = new CardPayment();
$payment1->pay(500);
$payment1->paymentInfo();
$payment2 = new UpiPayment();
$payment2->pay(1000);
?>

Explanation

  • Both CardPayment and UpiPayment extend Payment.
  • Both are forced to implement pay() method.
  • They follow the same structure but provide different logic.

This ensures design consistency across the application.

Important Rules of Abstract Classes

  1. You cannot create object of an abstract class.
    $obj = new Payment();
  2. If a class contains at least one abstract method, it must be declared abstract.
  3. Child classes must implement all abstract methods.
  4. Abstract methods cannot have a body.

Multiple Child Classes

<?php
class UpiPayment extends Payment {
    public function pay($amount) {
        echo "Paid ₹$amount using UPI";
    }
}
?>

Both classes follow the same structure but implement different payment logic.

Why Abstraction is Important?

Abstraction helps developers create a clear structure for large applications by separating required functionality from actual implementation.

It allows you to define common rules that all child classes must follow while still giving flexibility in how those rules are implemented.

Abstraction helps you:

  • Enforce consistency across multiple classes
  • Create scalable and maintainable architecture
  • Reduce code duplication
  • Separate design from implementation
  • Share common functionality while requiring specific behavior

This makes applications cleaner, easier to manage, and more professional.

Abstraction vs Encapsulation

ConceptPurpose
EncapsulationData protection
AbstractionDesign enforcement

Real-World Example

Imagine you are building a payment system.

You create an abstract class Payment.

It may contain:

  • abstract method processPayment()
  • normal method paymentReceipt()

Every payment method like:

  • CreditCard
  • PayPal
  • UPI

Must implement processPayment().

This ensures that every payment method follows a standard structure.

Abstract Class vs Normal Class

FeatureNormal ClassAbstract Class
Can create object?YesNo
Can contain abstract method?NoYes
Can contain normal method?YesYes
Must be extended?Not necessaryYes

Common Mistakes

❌ Creating object of abstract class
❌ Not implementing abstract methods
❌ Mixing abstraction with concrete logic
❌ Forgetting abstract keyword

Practical Tasks

  • Create an abstract class called Shape.
  • Add:
    • abstract method calculateArea()
    • normal method displayShape()
  • Then create two child classes:
    • Circle
    • Rectangle
  • Implement calculateArea() in both classes.
  • Create objects and test them.

Summary

  • Abstract class cannot be instantiated
  • It may contain abstract and normal methods
  • Abstract methods must be implemented by child classes
  • It helps in creating structured and scalable applications

Abstract classes provide a blueprint for other classes and enforce consistent behavior.

In the next tutorial, we’ll learn about Interfaces in PHP.

Related Tutorials

  • OOP in PHP – Introduction to Object-Oriented Programming

    Introduction As PHP projects become larger, managing code using only procedural programming becomes difficult. Files get longer, functions become harder to track, and updating the project takes more time. This is where Object-Oriented Programming (OOP) becomes important. OOP helps developers organize code in a cleaner and more structured way by grouping related data and functionality…

  • Classes & Objects in PHP

    Introduction In Object-Oriented Programming (OOP), classes and objects are the foundation of everything. Once you understand these two concepts clearly, learning advanced OOP topics like constructors, inheritance, and polymorphism becomes much easier. In simple words, a class is a blueprint, and an object is a real instance created from that blueprint. For example, if “Car”…

  • Constructors & Destructors in PHP

    Introduction In Object-Oriented Programming (OOP), constructors and destructors are special methods that automatically run at specific moments in an object’s lifecycle. A constructor runs when an object is created, while a destructor runs when an object is destroyed. These methods help developers manage object initialization and cleanup efficiently. Instead of manually setting values after creating…

  • Access Modifiers in PHP (public, private, protected)

    Introduction Access modifiers in PHP control how properties and methods can be accessed inside Object-Oriented Programming (OOP). They help define the visibility of class members and protect important data from unwanted access. With access modifiers, you can decide who can access a property, who can call a method, and whether a value should be changed…

  • Inheritance in PHP

    Introduction Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows one class to inherit the properties and methods of another class. In simple words, inheritance helps you reuse existing code instead of rewriting it. This makes your code cleaner, more structured, and easier to maintain. In this tutorial, you will…

  • Encapsulation in PHP

    Introduction Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It refers to the concept of restricting direct access to certain parts of an object and allowing controlled access through methods. In simple words, encapsulation means data hiding. Instead of allowing users to directly access and modify object properties, we protect them using…

Leave a Reply

Your email address will not be published. Required fields are marked *