Abstract Class in PHP with Examples
An abstract class in PHP is a class, which is declared with an abstract keyword. It is similar to a normal class, but has three main differences:
1. Object Creation:
- An abstract class cannot be instantiated directly, meaning we cannot create an object of it.
- Only objects of its non-abstract (concrete) subclasses can be created.
2. Abstract Methods:
- An abstract class may contain zero or more abstract methods.
- An abstract method is a method that is declared but not implemented in the abstract class. The implementation must be done by its subclasses.
- A normal (concrete) class cannot contain abstract methods.
3. Level of Abstraction:
- An abstract class can contain both abstract and concrete methods, allowing partial abstraction. However, it can also provide full abstraction when all its methods are abstract.
- A normal class provides no abstraction because all its methods must be fully implemented in the class.
The purpose of an abstract class in PHP is to define a common base structure, enforce method implementation in subclasses, and share functionality while preventing direct instantiation. These key differences make abstract classes a powerful feature in PHP object-oriented programming that helps to build flexible, maintainable, and well-structured applications.
Syntax of Abstract Class in PHP
The basic syntax of defining abstract class in PHP is as:
<?php
abstract class Vehicle {
// Abstract method (without implementation)
abstract public function startEngine();
// Concrete method (with implementation)
public function getDetails() {
return "This is a vehicle.";
}
// Property
protected $model;
// Constructor
public function __construct($model) {
$this->model = $model;
}
}
?>In the above syntax,
- The class Vehicle is declared using the abstract keyword.
- An abstract class may contain:
- Abstract methods (without implementation)
- Concrete methods (with implementation)
- Properties
- Constructors
Rules of Abstract Class in PHP
There are the following rules of using an abstract class in PHP:
- Abstract classes cannot be instantiated directly using the new keyword.
- An abstract class may contain both abstract and concrete methods. It may also include properties and constants.
- Abstract methods must not have a body. They can be declared as public or protected.
- Any non-abstract (concrete) child class that extends an abstract class must implement all abstract methods defined in the abstract class.
- An abstract class can extend another abstract class.
How to Implement Abstract Class in PHP?
To implement an abstract class in PHP, every non-abstract (or concrete) child class that extends an abstract class must implement all of its abstract methods; otherwise, PHP will throw a fatal error. Look at the complete code below.
Example 1:
<?php
abstract class Vehicle {
// Abstract method (no implementation)
abstract public function startEngine();
// Concrete method (with implementation)
public function getDetails() {
return "This is a vehicle.";
}
// Property
protected $model;
// Constructor
public function __construct($model) {
$this->model = $model;
}
}
// Child class
class Car extends Vehicle {
// Implementing the abstract method in the child class.
public function startEngine() {
return "Car engine started.";
}
// Overriding the concrete method inside the child class.
public function getDetails() {
return "This is a {$this->model} car.";
}
}
// Creating object of child class.
$myCar = new Car("Toyota");
// Calling methods.
echo $myCar->startEngine();
echo "\n";
echo $myCar->getDetails();
?>Output:
Car engine started. This is a Toyota car.
In this example:
- We have declared an abstract class Vehicle using abstract keyword. This class contains:
- Asbtract method startEngine()
- Concrete method getDetails()
- Property $model
- Constructor
- The method startEngine() is abstract method, so it must be implemented in the child class.
- The method getDetails() is a concrete method in the abstract class.
- The property $model is declared as protected, so it is accessible in the child class.
- The constructor defined inside the abstract class initializes the $model property.
- The Car class extends the abstract class Vehicle.
- The Car class implements the abstract method startEngine() and overrides the inherited getDetails() method.
- When a method is overridden, the getDetails() method of child class is executed at runtime.
- Since we cannot create an instance of abstract class, we have created an instance of its subclass.
Example 2: Abstract Method with Argument
<?php
abstract class Calculator {
// Abstract method with arguments.
abstract public function calculate($a, $b);
}
class Addition extends Calculator {
// Implementing the abstract method inside the child class.
public function calculate($a, $b) {
return $a + $b;
}
}
class Subtraction extends Calculator {
// Implementing the abstract method inside the child class.
public function calculate($a, $b) {
return $a - $b . "\n";
}
}
$add = new Addition();
$sub = new Subtraction();
// Calling the method with passing arguments.
echo $add->calculate(10, 20);
echo $sub->calculate(20, 10);
?>Output:
30 10
In this example:
- Calculator is an abstract class, so we cannot create its object.
- This class contains an abstract method calculate($a, $b) with arguments.
- The Addition class and Subtraction class extend the abstract class Calculator.
- Both classes implement the abstract method calculate() with their own implementations.
- The method parameters in the child class must match the abstract method.
- We have created objects of Addition and Subtraction classes and called methods with passing arguments.
Example 3: Cannot Create Instance of an Abstract Class in PHP
<?php
// Abstract class
abstract class Animal {
// Abstract method
abstract public function sound();
}
// This will cause a fatal error.
$animal = new Animal();
?>In this example:
- Animal is declared as an abstract class. So, PHP does not allow creating an object of an abstract class.
- If you try to create an object of abstract class, PHP generates a fatal error.
- You can only create an object of concrete (non-abstract) child classes that extend abstract class, not abstract class itself.
Example 4: Payment Processing System (Real-World Example)
<?php
abstract class Payment {
abstract public function pay($amount);
public function paymentStatus() {
return "Payment processed successfully.";
}
}
class CreditCardPayment extends Payment {
// Implementing abstract method inside the child class CreditCardPayment.
public function pay($amount) {
return "Paid ₹{$amount} using Credit Card.";
}
}
class UPIPayment extends Payment {
// Implementing abstract method inside the child class UPIPayment.
public function pay($amount) {
return "Paid ₹{$amount} using UPI.";
}
}
// Creating objects of child classes.
$credit = new CreditCardPayment();
$upi = new UPIPayment();
echo $credit->pay(500);
echo "\n";
echo $upi->pay(1000);
echo "\n";
echo $credit->paymentStatus();
?>Output:
Paid ₹500 using Credit Card. Paid ₹1000 using UPI. Payment processed successfully.
In this example:
- Payment is an abstract class, which defines an abstract method pay($amount) and a concrete method paymentStatus().
- The child classes CreditCardPayment and UPIPayment extend the abstract class Payment.
- Both child classes implement the abstract method pay() with their own specific implementations.
This is a real-world example of using abstract class in PHP, where multiple payment methods are following one common structure. You can also add new payment methods in future if necessary without changing existing code. As a result, using an abstract class improves maintainability, scalability, and overall code clarity.
When to Use Abstract Class in PHP?
You should use an abstract class when you need to:
- Share common base functionality among related classes.
- Provide partial implementation with combining abstract and concrete methods.
- Enforce method implementation in child classes.
- Achieve code reusability while maintaining a consistent structure.
Avoid These Mistakes:
- Don’t create abstract classes without at least one abstract method.
- Don’t forget to implement all abstract methods in child classes that extend the abstract class.
- Don’t overuse abstract classes when interfaces would suffice.
- Don’t make abstract classes too large or complex.
- Don’t try to instantiate abstract class directly.
- Don’t change method signature in the child class.



