Understanding Abstract Classes in PHP
- Published on
- -4 mins read
- Authors
- Name
- Muhammad Iqbal
- @dibaliqaja
Abstract Classes in PHP
An abstract class in PHP is a class that cannot be instantiated directly and is often only partially implemented. It typically contains at least one abstract method — a method without a body. Instead of implementation, it only defines the method name and parameters and is marked with the abstract keyword.
Abstract methods act as a contract. They tell us that any child class extending the abstract class must implement those methods.
Here’s a simple example:
<?php
abstract class Brew{ abstract protected function make(); protected function addWater() { return $this->make(); }
protected function addSugar() { return $this->make(); }}At this point, you might wonder:
How do we know when a method should be abstract?
Let’s walk through a practical example.
A Real Case: Tea and Coffee
Imagine we create a class to make tea:
<?php
class Tea{ public function addTea() { var_dump("Dip a tea"); return $this; }
public function addWater() { var_dump("Pour water"); return $this; }
public function addSugar() { var_dump("Add proper amount of sugar"); return $this; }
public function make() { return $this ->addTea() ->addWater() ->addSugar(); }}
$tea = new Tea();$tea->make();Now let’s create another class for coffee:
<?php
class Coffee{ public function addCoffee() { var_dump("Add coffee"); return $this; }
public function addWater() { var_dump("Pour water"); return $this; }
public function addSugar() { var_dump("Add proper amount of sugar"); return $this; }
public function make() { return $this ->addCoffee() ->addWater() ->addSugar(); }}
$coffee = new Coffee();$coffee->make();If you look closely, both classes share similar methods like addWater() and addSugar().
This is a clear sign of code duplication, which can make your code harder to maintain over time.
Refactoring with an Abstract Class
To clean this up, we can extract the shared logic into an abstract class:
<?php
abstract class Brew{ protected abstract function seasoning();
public function make() { return $this ->seasoning() ->addWater() ->addSugar(); }
public function addWater() { var_dump("Pour water"); return $this; }
public function addSugar() { var_dump("Add proper amount of sugar"); return $this; }}
class Tea extends Brew{ public function seasoning() { var_dump("Dip a tea"); return $this; }}
$tea = new Tea();$tea->make();
class Coffee extends Brew{ public function seasoning() { var_dump("Add coffee"); return $this; }}
$coffee = new Coffee();$coffee->make();Why This Approach Works
By introducing the abstract class Brew, we:
- Eliminate duplication by centralizing shared methods like
addWater()andaddSugar() - Enforce consistency using the abstract method
seasoning() - Improve readability with a cleaner and more structured design
- Make the code easier to extend when adding new drink types
Each child class (Tea, Coffee) now only focuses on what makes it unique.
When Should You Use Abstract Classes?
Use abstract classes when:
- Multiple classes share common behavior
- You want to enforce a consistent structure across child classes
- You need a base class that should never be instantiated directly
- You want to reduce duplication and improve maintainability
Final Thoughts
Abstract classes are a powerful tool in object-oriented programming, especially when building scalable and maintainable applications. They help you define clear contracts while keeping your code DRY (Don’t Repeat Yourself).
Once you start recognizing patterns like duplicated methods across classes, that’s usually a good time to consider introducing an abstract class.
That’s all. I hope this was helpful. See you later.