Skip to main content

Abstract Classes

Chapter Overview

In this chapter you will learn:

  • What abstract classes are
  • Why abstract classes exist
  • Abstract methods
  • Concrete methods
  • Constructors inside abstract classes
  • Properties in abstract classes
  • Difference between interfaces and abstract classes
  • Real world examples
  • PocketMine architecture
  • Enterprise architecture
  • Best practices
  • Exercises and projects

What is an Abstract Class?

An abstract class is a class that:

Cannot be instantiated directly
and is designed to be inherited.

It acts as a blueprint.


Definition

Abstract classes allow you to:

✅ Define common functionality

✅ Force child classes to implement methods

✅ Share code between classes


Syntax

abstract class Animal {

}

Keyword:

abstract

Invalid Example

$animal =
new Animal();

Output:

Fatal Error
Cannot instantiate abstract class

Why Abstract Classes Exist

Imagine:

Dog
Cat
Bird
Fish

All animals:

  • Eat
  • Sleep
  • Move

But every animal has different sounds.


Instead of:

class Dog {

}

class Cat {

}

class Bird {

}

repeating code, we create:

abstract class Animal {

}

Real Life Example

Think of:

Vehicle

You never see:

Generic Vehicle

You only see:

  • Car
  • Bike
  • Truck

Vehicle itself is abstract.


Visualization

Vehicle

├── Car
├── Bike
└── Truck

Basic Example

abstract class Animal {

public function eat() {

echo "Eating";

}

}

Child:

class Dog
extends Animal {

}

Usage:

$dog =
new Dog();

$dog->eat();

Why Not Create Animal?

Because:

Animal itself is only an idea.

Concrete objects are:

Dog
Cat
Bird

Abstract Methods

Abstract methods have:

NO IMPLEMENTATION

Example:

abstract class Animal {

abstract public function sound();

}

No body:

{

}

Child Must Implement

class Dog
extends Animal {

public function sound() {

echo "Bark";

}

}

Another:

class Cat
extends Animal {

public function sound() {

echo "Meow";

}

}

Visualization

Animal

├── eat()
├── sleep()
└── sound() ← abstract

Why?

Because every animal:

Sounds differently.

Mixing Abstract and Normal Methods

Abstract classes may contain:

✅ Normal Methods

✅ Properties

✅ Constructors

✅ Constants

✅ Abstract Methods


Example

abstract class Animal {

protected string $name;

public function eat() {

echo "Eating";

}

abstract public function sound();

}

Properties Inside Abstract Classes

Example:

abstract class Entity {

protected int $health = 20;

}

Children inherit them.


Constructor Example

abstract class Entity {

public function __construct() {

echo "Entity Created";

}

}

Child:

class Player
extends Entity {

}

Output:

Entity Created

Abstract Classes as Templates

Abstract classes provide:

Default behavior
+
Required behavior

Real World Example


Payment System

Abstract class:

abstract class Payment {

public function validate() {

echo "Validation";

}

abstract public function pay();

}

Child:

class StripePayment
extends Payment {

public function pay() {

}

}

Another:

class PaypalPayment
extends Payment {

public function pay() {

}

}

Benefits

Validation code remains shared.

Only payment logic changes.


Database Example

abstract class Database {

protected string $host;

abstract public function connect();

}

Implementations:

MySQL
SQLite
MongoDB

Game Example

abstract class Entity {

protected int $health;

abstract public function attack();

}

Implementations:

Zombie
Player
Cow
Skeleton

PocketMine Example

PocketMine architecture heavily uses abstraction.


Simplified Example

Entity

├── Human
│ └── Player

├── Monster
│ └── Zombie

└── Animal

Entity behaves almost like an abstract concept.

Every entity:

Has Position
Has Health
Can Move
Can Teleport

But every entity:

Attacks Differently
Moves Differently
Behaves Differently

Plugin Example

PluginBase

Main

PluginBase acts as a framework blueprint.


Enterprise Example

Controller

UserController

AdminController

Shared functionality exists inside base classes.


Abstract Class vs Interface

This is extremely important.


FeatureInterfaceAbstract Class
Properties
Constructors
Method Bodies
Multiple Usage
State Storage

Example

Interface:

interface Logger {

public function log();

}

Abstract:

abstract class Logger {

protected string $path;

abstract public function log();

}

When To Use Interface?

When classes only share:

Behavior

When To Use Abstract Class?

When classes share:

Behavior
+
State
+
Implementation

Example

Bad:

interface Entity {

protected int $health;
}

Invalid.

Interfaces cannot have properties.


Good:

abstract class Entity {

protected int $health;

}

Method Visibility Rules

Abstract methods may be:

public
protected

Never:

private

Child Visibility Rule

Child visibility cannot become stricter.

Bad:

abstract public function move();

Child:

protected function move()

Invalid.


Good

public function move()

Final + Abstract?

Invalid:

final abstract class Entity {

}

Contradictory.


Abstract Constants?

PHP allows constants:

public const VERSION = 1;

inside abstract classes.


Template Method Pattern

Abstract classes often implement:

Template Method Pattern

Example:

abstract class Game {

public function start() {

$this->load();

$this->run();

}

abstract protected function load();

abstract protected function run();

}

Children customize parts.


Visualization

Game

├── start()

├── load()
└── run()

Benefits of Abstract Classes


1. Code Reuse

Shared logic remains in one place.


2. Cleaner Architecture

Projects become easier.


3. Less Duplication

Write once.

Reuse everywhere.


4. Better Design

Provides templates.


Common Beginner Mistakes


Instantiating Abstract Classes

Bad:

new Animal();

Using Interfaces Everywhere

Sometimes abstract classes are better.


Large Inheritance Chains

Bad:

Entity

Living

Monster

HostileMonster

SuperMonster

Too complex.


Best Practices

✅ Keep abstract classes small.

✅ Put only common functionality.

✅ Use interfaces for contracts.

✅ Avoid deep inheritance.


Real Enterprise Example

BaseController

├── UserController
├── AdminController
└── ApiController

Laravel Example

Model

├── User
├── Product
└── Post

Model provides:

save()
delete()
update()

Children inherit everything.


PocketMine Example

Entity

├── Human
│ └── Player

├── Monster
└── Projectile

Exercises


Exercise 1

Create:

abstract class Vehicle

Methods:

move()
fuel()

Exercise 2

Create:

Animal
Dog
Cat
Bird

Exercise 3

Create:

Payment
PayPal
Stripe
Crypto

Mini Project

Create:

Entity System

Architecture:

Entity

├── Player
├── Zombie
├── Skeleton
└── Cow

Properties:

Health
Position
Name

Methods:

move()
attack()
damage()

Interview Questions


What is an abstract class?

A class that cannot be instantiated directly.


Can abstract classes have properties?

Yes.


Can abstract classes have constructors?

Yes.


Difference between interface and abstract class?

Interfaces define contracts.

Abstract classes define contracts + implementation.


Can abstract classes have normal methods?

Yes.


Summary

Abstract Classes provide:

✅ Shared State

✅ Shared Logic

✅ Templates

✅ Cleaner Architectures

✅ Better Framework Design


References

https://www.php.net/manual/en/language.oop5.abstract.php


Next Chapter

➡ Traits

➡ Static Members

➡ Final Keyword

➡ Magic Methods