Why Do We Use Abstract Classes?
Abstract classes are useful in scenarios where multiple classes share common behaviors but need different implementations for some methods. Here are some key reasons to use abstract classes in PHP:
- Code Reusability: Abstract classes allow defining common methods that can be shared among multiple child classes.
- Encapsulation: They help in organizing code better by ensuring that specific methods must be implemented in child classes.
- Polymorphism: Abstract classes allow defining a common interface for different classes, promoting flexibility in code design.
- Predefined Structure: It helps maintain a predefined structure for all child classes, reducing errors and inconsistencies.
Example of Abstract Class in PHP
Let's consider a real-world example where different animals make different sounds.
<?php
abstract class Animal {
abstract public function makeSound();
}
class Dog extends Animal {
public function makeSound() {
return "Bark";
}
}
class Cat extends Animal {
public function makeSound() {
return "Meow";
}
}
$dog = new Dog();
echo $dog->makeSound();
$cat = new Cat();
echo $cat->makeSound();
?>

You can also try this code with Online PHP Compiler
Run Code
Output
Bark
Meow
Explanation
- Animal is an abstract class with an abstract method makeSound().
- Dog and Cat classes extend Animal and provide their own implementations of makeSound().
- The method makeSound() is implemented in each child class with its unique sound output.
Facts About Abstract Classes in PHP
Here are some important facts about abstract classes in PHP:
1. Cannot be Instantiated: An abstract class cannot be directly instantiated.
$animal = new Animal(); // Error: Cannot instantiate abstract class Animal
2. May Contain Non-Abstract Methods: Abstract classes can have regular methods with defined implementations.
abstract class Vehicle {
public function startEngine() {
return "Engine started";
}
}
3. Child Classes Must Implement All Abstract Methods: If a child class does not implement all abstract methods, it must also be declared as abstract.
abstract class Shape {
abstract public function draw();
}
class Circle extends Shape {
public function draw() {
return "Drawing a Circle";
}
}
4. Can Have Properties: Abstract classes can contain properties like normal classes.
abstract class Fruit {
public $color;
}
5. Can Have Constructors: Abstract classes can have constructors to initialize values.
abstract class Plant {
protected $type;
public function __construct($type) {
$this->type = $type;
}
}
How to Declare Classes and Methods as Abstract?
To start with abstract classes in PHP, you first need to know how to declare them. An abstract class is declared using the abstract keyword. This keyword tells PHP that the class cannot be instantiated directly. Instead, it must be extended by other classes.
For example:
<?php
abstract class Animal {
// Abstract method
abstract public function makeSound();
}
?>
In this example, Animal is an abstract class. It has one abstract method called makeSound(). The abstract keyword is used before the method declaration to indicate that this method must be implemented by any class that extends Animal.
Now, let’s see how to extend this abstract class. When you extend an abstract class, you must provide implementations for all the abstract methods.
For example:
<?php
class Dog extends Animal {
public function makeSound() {
return "Woof!";
}
}
class Cat extends Animal {
public function makeSound() {
return "Meow!";
}
}
?>
In this example, Dog and Cat are concrete classes that extend the abstract Animal class. Each class provides its implementation of the makeSound() method.
Let’s see how you can use these classes:
<?php
$dog = new Dog();
$cat = new Cat();
echo $dog->makeSound();
echo $cat->makeSound();
?>

You can also try this code with Online PHP Compiler
Run CodeOutputs:
Woof!
Meow!
When you create an instance of Dog or Cat, you can call the makeSound() method, and it will return the appropriate sound for each animal.
Frequently Asked Questions
Can an abstract class have a constructor in PHP?
Yes, an abstract class can have a constructor. The constructor can initialize properties that will be inherited by child classes.
What happens if a child class does not implement an abstract method?
If a child class does not implement all abstract methods from the parent abstract class, it must be declared as abstract itself.
Can an abstract class have normal methods?
Yes, an abstract class can have both abstract and non-abstract methods. Non-abstract methods have complete implementations that can be used by child classes.
Conclusion
Abstract classes in PHP are a fundamental part of object-oriented programming. They provide a way to enforce a structure for child classes while allowing flexibility in implementing specific methods. By using abstract classes, developers can write cleaner, more maintainable code. Understanding abstract classes is essential for PHP developers, especially when working on large-scale applications.