Table of Contents:
Abstract Classes in PHP OOP – Complete Beginner-Friendly Guide
Abstract classes are a key concept in Object-Oriented Programming (OOP) in PHP. They provide a powerful way to structure large applications by defining a common blueprint that child classes must follow.
Abstract classes help you:
- Enforce rules
- Create strong application architecture
- Avoid code repetition
- Build scalable, maintainable systems
This guide explains everything in simple English with examples, output, SEO keywords, and clarity.
What Is an Abstract Class in PHP?
An abstract class is a class that cannot be instantiated and is meant to be extended by other classes.
It may contain:
- Abstract methods (no body)
- Normal methods (with body)
- Properties
- Constants
Abstract classes act like a base structure for child classes.
abstract class ClassName {
abstract public function methodName();
}
Why Use Abstract Classes in PHP OOP?
Developers use abstract classes to:
- Enforce consistent method structure
- Share common logic among multiple child classes
- Improve code reusability
- Avoid duplicate code
- Follow strong OOP rules
They are widely used in modern PHP frameworks like Laravel, Symfony, and CodeIgniter.
Understanding Abstract Methods in PHP OOP
Abstract methods:
- Have no body
- Must be implemented by the child class
- Must be public or protected
- Force developers to follow the required structure
Example:
abstract class Test {
abstract public function demo();
}
Simple Abstract Class Example in PHP (With Output)
<?php
abstract class Animal {
abstract public function sound();
public function info() {
echo "Animals make different sounds<br>";
}
}
class Dog extends Animal {
public function sound() {
echo "Dog Barks<br>";
}
}
$pet = new Dog();
$pet->info();
$pet->sound();
?>
Output:
Animals make different sounds
Dog Barks
This example shows:
- Abstract method must be implemented
- Normal method can be inherited directly
Powerful Features of Abstract Classes in PHP
- Allow both defined and undefined methods
- Can have constructors
- Support properties and constants
- Cannot be instantiated directly
- Force structure while allowing flexibility
Abstract Class with Constructor Example
<?php
abstract class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function showRole();
}
class Student extends Person {
public function showRole() {
echo "Student Name: $this->name";
}
}
$obj = new Student("Amit");
$obj->showRole();
?>
Output:
Student Name: Amit
Abstract Class with Multiple Abstract Methods
<?php
abstract class Shape {
abstract public function area();
abstract public function perimeter();
}
class Circle extends Shape {
public $radius;
public function __construct($r) {
$this->radius = $r;
}
public function area() {
return 3.14 * $this->radius * $this->radius;
}
public function perimeter() {
return 2 * 3.14 * $this->radius;
}
}
$c = new Circle(5);
echo "Area: " . $c->area() . "<br>";
echo "Perimeter: " . $c->perimeter();
?>
Output:
Area: 78.5
Perimeter: 31.400000000000002
Key Rules of Abstract Classes in PHP
- You cannot create objects of abstract classes
- If a class contains at least one abstract method → class must be abstract
- Child classes must implement all abstract methods
- Abstract methods cannot be private
- Abstract class may have non-abstract methods
- Abstract classes support inheritance
Difference Between Abstract Class and Interface (SEO Comparison)
| Feature | Abstract Class | Interface |
|---|---|---|
| Contains method body | Yes | No |
| Contains properties | Yes | No |
| Multiple inheritance | No | Yes |
| Constructor allowed | Yes | No |
| Usage | Base structure + logic | Contract blueprint |
PHP abstract classes, abstract method PHP, PHP OOP abstract class example, abstract vs interface PHP, PHP OOP guide, PHP abstract keyword, PHP inheritance, object oriented PHP tutorial, advanced OOP PHP
Real-Life Use Cases of Abstract Classes in PHP
Abstract classes are used in:
- Payment gateway classes (Base gateway → PayPal, Stripe, Razorpay)
- Database models
- Notification systems (Email, SMS, Push)
- File storage systems
- Controllers in frameworks
- Report generation modules
Example:
Laravel’s Controller class is abstract.
Best Practices for Using Abstract Classes
- Use abstract classes when child classes share common behavior
- Keep abstract classes general, not specific
- Avoid adding unnecessary abstract methods
- Use interfaces when you only need a contract
- Prefer composition when inheritance gets too deep
Following these practices ensures your application stays scalable.
Read this to Learn another important concept here:
Frequently Asked Questions (FAQ)
1. Can abstract classes contain normal methods?
Yes, they can contain both abstract and normal methods.
2. Can we create an object of an abstract class?
No. Abstract classes cannot be instantiated.
3. Can abstract methods be private?
No. They must be public or protected.
4. Can abstract classes have properties?
Yes, unlike interfaces.
5. Can we use constructors in abstract classes?
Yes, constructors are allowed.
