Abstract Classes
Last updated onPHP Abstract classes let you define shared traits while allowing each part to be unique. giving you an organized structure of code, and adaptable programming.
In this tutorial, you will understand what are abstract classes and how to use it. Let's get started.
Understanding PHP Abstract Classes
An abstract class is like a set of instructions you give to other classes. It is a setting up guidelines— So that any class “built” from this abstract class has to follow.
But here you can not create an object from an abstract class directly. Instead, you create other classes that extend this abstract class, and these classes fill in the details.
Let's take a look at a quick example:
abstract class Animal {
// Abstract method (no body)
abstract public function makeSound();
// Regular method with full code
public function breathe() {
return "Breathing...";
}
}
class Dog extends Animal {
public function makeSound() {
return "Bark!";
}
}
// Create a Dog object
$dog = new Dog();
echo $dog->makeSound(); // Output: Bark!
echo $dog->breathe(); // Output: Breathing...
Look at the above example and check the following list:
is an abstract class, meaning it’s like a model or template.Animal
- It has an abstract method
, which has no body; this must be implemented by any child class.makeSound()
- There’s also a regular
method
breathe()
that is fully defined, meaning any child class (like
) will inherit this functionality.Dog
So the question is, Why Use Abstract Classes?
If you’re creating a bunch of similar objects in a project—different kinds of users on a site or various types of animals in a simulation—they will have shared actions.
But each type might also need to behave differently in certain situations. Abstract classes let you define those common actions just once, saving you time and reducing duplicate code.
For instance, you might have an abstract class called User
with a shared
method, but leave the specifics of other actions (like login()
for a social media site) up to the individual classes (like postContent()
, Admin
, etc.).Subscriber
Anyway, in the following section, you will see an example.
Example of Using Abstract Classes
Consider that you are building an online store with multiple payment options—credit cards, PayPal, and so on. Each payment method needs similar steps: validate details, process payment, and confirm the transaction.
Here are the benefits of abstract class—it lets you set up the shared steps in one place and customize each payment type as needed.
Let's see that in example:
abstract class PaymentProcessor {
abstract protected function validatePaymentDetails();
public function processPayment() {
// Common steps for any payment method
$this->validatePaymentDetails();
$this->deductAmount();
$this->confirmTransaction();
}
protected function deductAmount() {
echo "Amount deducted.\n";
}
abstract protected function confirmTransaction();
}
class CreditCardPayment extends PaymentProcessor {
protected function validatePaymentDetails() {
echo "Validating credit card details.\n";
}
protected function confirmTransaction() {
echo "Transaction confirmed via credit card.\n";
}
}
$payment = new CreditCardPayment();
$payment->processPayment();
This structure makes adding a new payment option a breeze. Just extend
, fill in the blanks for the unique steps. So, no need to rewrite the basics!PaymentProcessor
Here you will find the differences between abstract classes and Interfaces. Anyway, in the following section
Wrapping Up
PHP abstract classes give your code a foundation but leave room for customization. With an abstract class, you set the expectations (methods to be implemented) without dictating every little detail.
Here is a quick recap:
- Abstract classes are templates with some shared code and some requirements.
- They help you write DRY (Don’t Repeat Yourself) code and make it easy to add new elements to a project.
Thank you for reading. Happy Coding!
Frequently Asked Questions (FAQs)
What is a 'PHP abstract class'?
How do you define an abstract class in PHP?
Can I create an instance of an abstract class in PHP?
What’s the purpose of an abstract method in PHP?
How do I use an abstract class with child classes?
Why use abstract classes in PHP?
How is an abstract class different from an interface?
What’s an example of an abstract class in a payment system?
How do abstract classes help with DRY (Don’t Repeat Yourself) principles?
Can an abstract class have regular methods in PHP?