Last Updated: Feb 2, 2025
Easy

Abstract Classes in PHP

Author Sinki Kumari
0 upvote
Table of contents
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Abstract classes in PHP are classes that cannot be instantiated on their own. Instead, they serve as a blueprint for other classes. An abstract class can contain both abstract methods (methods without implementation) and concrete methods (methods with implementation). A class that extends an abstract class must implement all the abstract methods defined in the parent class.

Abstract Classes in PHP

In this article, we will learn about the syntax of abstract classes in PHP, how they are used to create certain structures in derived classes, and best practices for utilizing them in object-oriented programming.

What are Abstract Classes in PHP?

An abstract class in PHP is a class that contains at least one abstract method. An abstract method is a method that is declared but not defined within the class. Instead, it must be implemented by any subclass that extends the abstract class.

Syntax of Abstract Class

abstract class Animal {
    abstract public function makeSound();
}
You can also try this code with Online PHP Compiler
Run Code
  • The abstract keyword is used before the class name to declare it as an abstract class.
     
  • The method makeSound() is declared as abstract, meaning any class that extends Animal must define this method.

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:

  1. Code Reusability: Abstract classes allow defining common methods that can be shared among multiple child classes.
     
  2. Encapsulation: They help in organizing code better by ensuring that specific methods must be implemented in child classes.
     
  3. Polymorphism: Abstract classes allow defining a common interface for different classes, promoting flexibility in code design.
     
  4. 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 Code

Outputs: 

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.