An abstract class in PHP is a special kind of class that serves as a template for other classes. It cannot be instantiated directly, meaning you cannot create an object from it. Instead, abstract classes are designed to define a common structure and behavior that subclasses must follow, while still allowing flexibility in implementation.
Key Characteristics of Abstract Classes in PHP
- Cannot be instantiated: You cannot create an object directly from an abstract class.
- Abstract methods: Declared without implementation. Subclasses extending the abstract class must implement them.
- Concrete methods: Abstract classes can also contain fully implemented methods, properties, and constants.
- Inheritance: Other classes can extend the abstract class and inherit its behavior, while also providing their own specific implementations.
Example in PHP
<?php
abstract class Vehicle {
// Abstract method (must be implemented by any subclass)
abstract public function startEngine(): void;
// Concrete method (can be used or overridden by subclasses)
public function stopEngine(): void {
echo "Engine stopped.\n";
}
}
class Car extends Vehicle {
public function startEngine(): void {
echo "Car engine started.\n";
}
}
class Motorcycle extends Vehicle {
public function startEngine(): void {
echo "Motorcycle engine started.\n";
}
}
// Usage
$car = new Car();
$car->startEngine(); // Output: Car engine started.
$car->stopEngine(); // Output: Engine stopped.
$bike = new Motorcycle();
$bike->startEngine(); // Output: Motorcycle engine started.
$bike->stopEngine(); // Output: Engine stopped.
Here, the abstract class Vehicle defines the overall structure:
startEngine() is abstract, so every subclass (Car, Motorcycle) must implement it.
stopEngine() is concrete, so it can be reused as is or overridden if needed.
Benefits of Using Abstract Classes in PHP
- Encapsulation of shared logic – Common code can be centralized in the abstract class, reducing duplication.
- Design consistency – Abstract methods enforce rules that all subclasses must follow.
- Flexibility – Combines mandatory implementations (abstract methods) with optional default behavior (concrete methods).
- Polymorphism – Different subclasses can be treated as the same type (e.g., all Vehicle objects), while still having their own implementations.
Use Cases in PHP
- Defining base models such as PaymentGateway, Vehicle, or Shape where certain functionality must differ between implementations.
- Creating framework components (for example, Symfony and Laravel use abstract classes heavily in their core).
- Enforcing coding standards in large teams or projects by making sure all subclasses share a consistent structure.
Abstract Class vs. Interface in PHP
While both abstract classes and interfaces define contracts for subclasses:
- Abstract classes can contain both abstract and concrete methods, properties, and constants.
- Interfaces (in PHP) can only define method signatures (though since PHP 8 they can include constants and default methods using traits).
Abstract classes are therefore more suitable when you need to share both structure and behavior, while interfaces are better for defining pure contracts.
In PHP, abstract classes are a powerful OOP feature that balance flexibility and control, allowing developers to create clean, reusable, and extensible code.