Abstract Class in PHP: How It Works & Examples

Abstract Classes in PHP

Abstract class in PHP appeared to provide a way to define a structure for related classes and don’t allow direct instantiation. They make sure that subclasses implement specific methods and improve code organization.

We will cover the following topics in this article:

  • The basic definition of abstract classes in PHP.
  • Extend abstract classes in PHP.
  • The difference between abstract classes and interfaces.

Let’s start with the basic definition.

What is an Abstract Class in PHP?

An abstract class in PHP acts as a template for other classes. You can’t create an object directly from it.

It includes abstract methods that declare what a method should do but leave the actual implementation to the child classes. Abstract classes can also have regular methods with full code inside them.

Here is a quick example:

abstract class Vehicle {
    // Abstract method (no implementation)
    abstract public function move();

    // Regular method
    public function stop() {
        echo "The vehicle stops.";
    }
}

class Bicycle extends Vehicle {
    public function move() {
        echo "The bicycle pedals forward.";
    }
}

$bicycle = new Bicycle();
$bicycle->move();  
$bicycle->stop();  

Output:

The bicycle pedals forward.The vehicle stops.

Here’s what happens:

  • Vehicle is an abstract class. It defines an abstract method move() and a regular method stop().
  • Bicycle extends Vehicle and provides the implementation for move().
  • You can call both move() and stop() on a Bicycle object.

Abstract classes make sure that child classes like Bicycle implement specific behaviors. It also allows shared functionality like stop().

Abstract classes in PHP define a base structure for related classes. They provide shared methods, while it requires subclasses to implement specific behaviors.

An abstract class uses the abstract keyword. It can contain both abstract methods (without a body) and concrete methods (with a body). Subclasses must implement all abstract methods.

Let’s move on to the following section to understand how to extend abstract classes in PHP.

How to Extend an Abstract Class in PHP

Create a subclass using the extends keyword. The subclass must implement all abstract methods from the parent class.

Here is an example:

abstract class Animal {
    abstract public function makeSound();
    
    public function sleep() {
        return "Sleeping...";
    }
}

class Dog extends Animal {
    public function makeSound() {
        return "Bark";
    }
}

$dog = new Dog();
echo $dog->makeSound();  
echo "\n";
echo $dog->sleep();  

Output:

Bark
Sleeping...

Here are steps to extend abstract classes:

  • Define an abstract class with the abstract keyword.
  • Include abstract methods (without a body) and concrete methods (with a body).
  • Create a subclass with extends.
  • Implement all abstract methods in the subclass.
  • Instantiate the subclass and use its methods.

You must decide how to handle abstract class methods when you extend them in PHP. So, the difference between abstract and concrete methods helps you implement them correctly. Let’s move to the following section to see how key differences.

Abstract Methods vs. Concrete Methods in PHP

PHP supports both abstract and concrete methods in abstract classes. Each serves a different purpose in structuring code.

Abstract Methods:

  • Declared with the abstract keyword.
  • Must be implemented in subclasses.
  • Cannot have a body (no implementation).
  • Enforce a required structure for all child classes.

Concrete Methods:

  • Have a defined implementation.
  • Can be inherited by subclasses.
  • Allow code reuse.
  • Subclasses can override them if needed.

Here are the key differences:

FeatureAbstract MethodConcrete Method
ImplementationNo bodyHas a body
Requires OverrideYesNo
Code ReusabilityNoYes

So, what are the differences between abstract classes and interfaces in PHP? Let’s answer this question in the following section.

Difference Between Abstract Classes and Interfaces in PHP

Abstract classes and interfaces set rules for other classes, but they work in different ways.

Abstract Classes:

  • Can have abstract and concrete methods.
  • Can have properties.
  • Use extends (single inheritance).
  • Methods can be public, protected, or private.
  • Best for shared behavior and structure.

Interfaces:

  • Only method signatures (before PHP 8.0).
  • Cannot have properties.
  • Use implements (multiple inheritance).
  • Methods must be public.

Wrapping Up

You learned how abstract classes define a base structure for related classes and enforce method implementation in PHP. You also saw how to extend abstract classes and compare abstract classes with interfaces.

Here is a quick recap:

  • Abstract classes provide a structure for subclasses and can have both abstract and regular methods.
  • It extends an abstract class and requires the child class to implement all abstract methods.
  • Abstract vs. concrete methods—abstract methods must be implemented, while concrete methods provide default behavior.
  • Abstract classes vs. interfaces—abstract classes allow properties and mixed methods, while interfaces enforce method signatures.

FAQ’s

What is an abstract class in PHP?

An abstract class in PHP acts as a template for other classes. You cannot create an object from it directly. It includes abstract methods that subclasses must implement and concrete methods that provide shared functionality.

How do you define an abstract class in PHP?

Use the abstract keyword before the class keyword. Inside the class, define abstract methods without a body and concrete methods with an implementation.
abstract class Vehicle {
    abstract public function move();
    
    public function stop() {
        echo "The vehicle stops.";
    }
}

Can an abstract class have properties in PHP?

Yes, an abstract class can have properties. Subclasses can inherit and use them.

Can an abstract class have both abstract and concrete methods?

Yes, abstract classes can mix abstract methods (which must be implemented by subclasses) and concrete methods (which already have an implementation).

How do you extend an abstract class in PHP?

Use the extends keyword in a subclass and implement all abstract methods from the parent class. Here is an example:
class Bicycle extends Vehicle {
    public function move() {
        echo "The bicycle pedals forward.";
    }
}

What happens if a subclass does not implement an abstract method?

PHP will throw a fatal error because subclasses must implement all abstract methods from the parent class.

Can a class extend multiple abstract classes in PHP?

No, PHP does not support multiple inheritance. A class can extend only one abstract class but can implement multiple interfaces.

Similar Reads

PHP array_any: How it Works with Arrays with Examples

PHP 8.4 released the array_any to test if at least one element matches a condition. Understand the array_any Function in…

PHP Object | How to Create an Instance of a Class

The PHP object is an instance from the PHP class or the main data structure that is already been created…

Inheritance in PHP: Share Code from Class to Class & Examples

PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions in…

PHP Global Variables & Superglobals: A Complete Guide

Variables in PHP are general elements used for data storage and other manipulations. Global variables belong to a particular category…

PHP File Inclusion: require, include, require_once, include_once

PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

PHP $_ENV: Manage Environment Variables

In this tutorial, we will explore a form of superglobal variable in PHP: $_ENV, which symbolizes how PHP reaches for…

PHP array_unshift: How to Add Values to the Start of an Array

PHP array_unshift helps you add new elements to appear before the existing ones. Understand the array_unshift function in PHP The…

PHP mb_substr Function: How it Works with Examples

You may need to get part of a text when you work with text in PHP. The mb_substr gives you…

Mastering PHP’s Do-While Loop: Examples and Explanation

The PHP do-while loop is a type of loop that executes a block of code at least once, and then…

PHP str_repeat: How it Works with Examples

PHP gives you many string tools. One of them is php str_repeat. This function repeats a texts as many times…

Previous Article

PHP Access Modifiers: How Public, Private & Protected Work

Next Article

PHP Namespace: How to Group Code (With Example)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.