An abstract class in PHP is a class that cannot be instantiated directly. Instead, it serves as a base class that other classes extend.
It can include both defined methods (with code) and abstract methods (without code).
The Syntax of Abstract Class Works in PHP
Here is the basic concept:
- Use the
abstract
keyword to define it. - Can include regular properties and methods.
- Must be extended by another
class
. - Cannot be used to create objects directly.
- Can include abstract methods—these have no body and must be implemented by child classes.
Here is an example:
abstract class SmartDevice {
abstract public function activate();
public function status() {
echo "Device is online";
}
}
Abstract classes define a shared structure for child classes. They help you to:
Make sure each child class defines the required methods.
Allow shared behavior across similar objects.
Keep code organized and reusable.
class SmartLight extends SmartDevice {
public function activate() {
echo "Light turned on\n";
}
}
$device = new SmartLight();
$device->activate();
$device->status();
Here is its output:
Light turned on
Device is online
This example shows you how SmartDevice
provides a base for different smart devices like lights
, thermostats
, or alarms
. Each subclass defines its own way to activate the device.
Abstract Class vs Interface in PHP
Abstract Class:
- Cannot be instantiated.
- Can include both abstract and normal methods.
- Can have properties (with values).
- A class can extend one abstract class.
- Can use
public
,protected
,private
. - Allows shared logic through defined methods.
Interface:
- Cannot be instantiated.
- Only method declarations.
- Cannot have properties (until PHP 8.1).
- A class can implement multiple interfaces.
- All methods are public.
- No shared logic, only structure.
So the question is: when to use each one?
Use an abstract class when:
You want to define shared behavior across related classes.
You need to include properties and method implementations.
If you want to provide a default implementation that can be reused or overridden.
Use an interface when:
You want to define a contract with no implementation.
If you need to enforce method structure across unrelated classes.
You need to implement multiple interfaces in one class.
How to Extend an Abstract Class in PHP
Apply the extends
keyword to create a subclass from an abstract class. The child class must implement all abstract methods from the parent class.
For example:
abstract class SmartDevice {
abstract public function activate();
public function status() {
echo "Device is online\n";
}
}
class SmartFan extends SmartDevice {
public function activate() {
echo "Fan turned on\n";
}
}
Here:
-
SmartDevice
is the abstract class. -
SmartFan
extends it and must defineactivate()
.
You can also override an abstract method when you provide its full definition in the child class.
The method name, parameters, and visibility must match.
class SmartSpeaker extends SmartDevice {
public function activate() {
echo "Speaker playing music\n";
}
}
You can also override non-abstract methods if needed:
class SmartSpeaker extends SmartDevice {
public function activate() {
echo "Speaker playing music\n";
}
public function status() {
echo "Speaker is connected\n";
}
}
The status()
is not abstract, but you can override it if behavior needs to change.
So, we can say:
- All abstract methods must be implemented.
- Method signature must match exactly.
- You can override regular methods if needed.
Abstract Class with Constructor in PHP
An abstract class can have a constructor. This lets you define shared setup logic for all child classes.
For example:
abstract class SmartDevice {
protected $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function activate();
public function status() {
echo "$this->name is online\n";
}
}
When a child class extends the abstract class, it can:
- Inherit the constructor as-is.
- Call the parent constructor using parent::__construct().
- Add extra setup logic in its own constructor.
class SmartThermostat extends SmartDevice {
private $temperature;
public function __construct($name, $temperature) {
parent::__construct($name);
$this->temperature = $temperature;
}
public function activate() {
echo "$this->name set to $this->temperature °C\n";
}
}
$device = new SmartThermostat("Living Room Thermostat", 22);
$device->activate();
$device->status();
Here is the output:
Living Room Thermostat set to 22 °C
Living Room Thermostat is online
- Abstract classes can have constructors.
- Use
parent::__construct()
to call the parent constructor. - Pass values through the child constructor as needed.
Wrapping Up
Abstract classes in PHP are classes that can’t be used to create an object on their own. They serve as a foundation for other classes to inherit from.
They can include both abstract methods (without code) and regular methods (with code). Any child class that extends an abstract class must implement the abstract methods.
Thank you for reading. If you would like to read more PHP tutorials, please visit our blog, FlatCoding or navigate to PHP manual.