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:
Paymentis 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
abstractkeyword - 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
abstractkeyword - 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
CardPaymentandUpiPaymentextendPayment. - 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
- You cannot create object of an abstract class.
❌$obj = new Payment(); - If a class contains at least one abstract method, it must be declared abstract.
- Child classes must implement all abstract methods.
- 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
| Concept | Purpose |
|---|---|
| Encapsulation | Data protection |
| Abstraction | Design 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
| Feature | Normal Class | Abstract Class |
|---|---|---|
| Can create object? | Yes | No |
| Can contain abstract method? | No | Yes |
| Can contain normal method? | Yes | Yes |
| Must be extended? | Not necessary | Yes |
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()
- abstract method
- 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.
