DEV Community

Patricia Hernandez
Patricia Hernandez

Posted on

Abstract Classes in PHP: What They Are and How to Use Them

PHP abstract classes help you define a base class that other classes must follow. They act like a blueprint. You can't create an object from an abstract class directly. But you can extend it and use its structure.

What Is an Abstract Class?

An abstract class contains abstract methods. These are methods declared but not fully defined. Child classes must define them.

You use the abstract keyword to define both abstract classes and abstract methods.

Why Use Abstract Classes?

  • Force child classes to follow a structure
  • Share common code across related classes
  • Build a reusable foundation

Let’s explore abstract classes with silly but clear examples.

Example 1: Abstract Class Boy

<?php

abstract class Boy {
    public $name;

    public function __construct($name) {
        $this->name = $name;
    }

    abstract public function play();

    public function sayHello() {
        echo "Hi, I'm $this->name\n";
    }
}

class FootballBoy extends Boy {
    public function play() {
        echo "$this->name is playing football\n";
    }
}

$jack = new FootballBoy("Jack");
$jack->sayHello();
$jack->play();

?>
Enter fullscreen mode Exit fullscreen mode

What’s Going On?

  • Boy is abstract.
  • It has an abstract method play().
  • FootballBoy extends Boy and defines play().
  • We can create a FootballBoy, but not a plain Boy.

Example 2: Abstract Class Girl

<?php

abstract class Girl {
    protected $hobby;

    public function __construct($hobby) {
        $this->hobby = $hobby;
    }

    abstract public function dance();

    public function showHobby() {
        echo "My hobby is $this->hobby\n";
    }
}

class BalletGirl extends Girl {
    public function dance() {
        echo "Dancing ballet gracefully\n";
    }
}

$anna = new BalletGirl("ballet");
$anna->showHobby();
$anna->dance();

?>
Enter fullscreen mode Exit fullscreen mode

Key Points

  • Abstract class helps share common logic (showHobby()).
  • It requires child classes to implement dance().

Example 3: Abstract Class Donkey

Yes, even donkeys can use OOP.

<?php

abstract class Donkey {
    abstract public function makeSound();

    public function walk() {
        echo "Donkey walking slowly...\n";
    }
}

class TalkingDonkey extends Donkey {
    public function makeSound() {
        echo "Hee-haw! I can talk!\n";
    }
}

$donkey = new TalkingDonkey();
$donkey->walk();
$donkey->makeSound();

?>
Enter fullscreen mode Exit fullscreen mode

What Did You Learn?

  • Donkey class gives a walk() method.
  • Every subclass must define makeSound().

Rules of Abstract Classes

  • You can't create objects from abstract classes.
  • Abstract methods can't have a body.
  • If a class has one abstract method, the whole class must be abstract.
  • A child must implement all abstract methods.

When Should You Use One?

Use an abstract class when:

  • You need to define a contract for child classes.
  • You want to avoid repeating the same code.
  • You need partial implementation and want subclasses to finish the job.

Final Thoughts

Abstract classes in PHP make your code structured, reusable, and clean. It’s like giving your classes a rulebook. With fun examples like Boy, Girl, and Donkey, you can understand how it works without stress.

Now go write your own abstract classes—and maybe teach your own donkey to code.

Top comments (1)

Collapse
 
xwero profile image
david duymelinck • Edited

Two of the three examples don't have any extra information.

I would compare it with the alternatives; interfaces and a regular class.
For example throwing an error in a regular class method when it is not implemented has a similar effect than an abstract method.

class Boy 
{
  public function play()
  {
      throw new \Exception('Play method is not implemented');
  }
}
Enter fullscreen mode Exit fullscreen mode