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.
Next Article

PHP OOP Programming: A Complete Tutorial

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.