W3docs

abstract

In PHP, the abstract keyword is used to define abstract classes and abstract methods. An abstract class is a class that cannot be instantiated and is meant to

The abstract Keyword in PHP

The abstract keyword marks a class or method as incomplete on purpose. An abstract class defines a common shape and shared behaviour but cannot be turned into an object on its own — it exists to be extended. An abstract method declares a method's signature without a body, forcing every concrete subclass to supply the actual implementation.

This page covers what abstract classes and methods are, how to declare them, the rules PHP enforces, and when reaching for abstract is the right design choice.

What is an Abstract Class?

An abstract class is a class you cannot instantiate directly. It is meant to be extended by other classes that fill in the missing pieces. An abstract class can mix two kinds of members:

  • Abstract methods — declared but not implemented, leaving the work to subclasses.
  • Concrete methods and properties — fully implemented and inherited as-is, so shared logic lives in one place.

The rule is one-directional: a class with at least one abstract method must be declared abstract. The reverse is not required — a class can be abstract without having any abstract methods, simply to prevent it from being instantiated.

<?php

abstract class Animal {
    // Concrete method — inherited by every subclass unchanged
    public function describe(): string {
        return "I am a " . static::class . " and I say " . $this->makeSound();
    }

    // Abstract method — each subclass must implement it
    abstract public function makeSound(): string;
}

What is an Abstract Method?

An abstract method is declared with the abstract keyword, has no body, and ends with a semicolon instead of { ... }. Its job is to define a contract: any subclass that wants to be concrete has to provide a matching implementation.

<?php

abstract public function makeSound(): string; // signature only, no body

A subclass must keep the method compatible with the abstract declaration — the same (or compatible) parameters and a visibility that is equal to or less restrictive than the original.

How to Define an Abstract Class and Method

Put abstract before class to make the class abstract, and before function to make a method abstract. Here is the full pattern — a base class with one shared method and one abstract method, plus two concrete subclasses:

<?php

abstract class Animal {
    public function describe(): string {
        return static::class . " says " . $this->makeSound();
    }

    abstract public function makeSound(): string;
}

class Dog extends Animal {
    public function makeSound(): string {
        return "Woof!";
    }
}

class Cat extends Animal {
    public function makeSound(): string {
        return "Meow!";
    }
}

echo (new Dog())->describe(), "\n"; // Dog says Woof!
echo (new Cat())->describe(), "\n"; // Cat says Meow!

Each subclass implements makeSound() and inherits describe() for free — that shared method is exactly why an abstract base class is more useful than a plain interface here.

What Happens If You Instantiate an Abstract Class?

Trying to create an object from an abstract class is a fatal error, caught at compile time:

<?php

abstract class Animal {
    abstract public function makeSound(): string;
}

$a = new Animal();
// Fatal error: Cannot instantiate abstract class Animal

The same error occurs if a subclass forgets to implement an inherited abstract method — PHP treats that subclass as still abstract.

Rules and Gotchas

  • A class with any abstract method must be declared abstract.
  • Abstract methods cannot be private — subclasses could not see them to override. Use public or protected.
  • Abstract methods cannot be final (they are meant to be overridden) and, since PHP 8.0, may not be static in a meaningful overridable way — prefer instance abstract methods.
  • An abstract class can have a constructor; subclasses call it with parent::__construct().
  • A subclass that does not implement every inherited abstract method must itself be declared abstract.

Abstract Class vs Interface

Both define a contract, but they answer different questions:

  • An abstract class ("is-a") can carry concrete methods, properties, and constructors. A class can extend only one abstract class.
  • An interface ("can-do") declares method signatures only (no implementation, no instance state) and a class can implement many interfaces.

Reach for an abstract class when subclasses share real implementation; reach for an interface when you only need to guarantee a capability. See PHP Inheritance and PHP Interfaces for more.

Conclusion

The abstract keyword lets you define a partial blueprint: an abstract class supplies shared behaviour while declaring abstract methods that every concrete subclass must implement. You cannot instantiate an abstract class directly — that is the point. Use it when several related classes share logic but each needs to customise specific steps, and choose an interface instead when no shared implementation is involved.

Practice

Practice
What are the rules regarding abstract classes and methods in PHP as explained on w3docs.com?
What are the rules regarding abstract classes and methods in PHP as explained on w3docs.com?
Was this page helpful?