Skip to main content

Inheritance

Inheritance is one of the Four Pillars of Object Oriented Programming.

Inheritance allows one class to acquire properties and methods from another class.

Think of inheritance as:

Parent → Child Relationship

Real Life Example

Animal

├── Dog
├── Cat
└── Bird

All animals have:

  • Name
  • Age
  • Eat()
  • Sleep()

Instead of rewriting this code multiple times, child classes inherit it.


Why Inheritance Exists

Without inheritance:

class Dog {

public string $name;

public function eat() {}

}
class Cat {

public string $name;

public function eat() {}

}

Code duplication.


With inheritance:

class Animal {

public string $name;

public function eat() {}

}
class Dog extends Animal {

}
class Cat extends Animal {

}

Cleaner and reusable.


Basic Syntax

Parent:

class Animal {

}

Child:

class Dog
extends Animal {

}

Keyword:

extends

means:

Inherit everything from parent.

Example

class Animal {

public function eat() {

echo "Eating";

}

}

Child:

class Dog
extends Animal {

}

Usage:

$dog =
new Dog();

$dog->eat();

Output:

Eating

Visualization

Animal

├── name
├── age
└── eat()



Dog

Dog automatically receives everything.


Inheriting Properties

Parent:

class Human {

public string $name;

}

Child:

class Player
extends Human {

}

Usage:

$player =
new Player();

$player->name =
"Aayan";

Works perfectly.


Inheriting Methods

class Human {

public function hello() {

echo "Hello";

}

}

Child:

class Player
extends Human {

}

Usage:

$player->hello();

Parent and Child Relationship

Parent

Child

Child:

✅ Can access parent public members.

✅ Can access protected members.

❌ Cannot access private members.


Access Modifier Rules

ModifierParentChild
public
protected
private

Example

class Human {

protected int $health = 20;

}

Child:

class Player
extends Human {

public function heal() {

$this->health++;

}

}

Valid.


Private Example

class Human {

private int $health = 20;

}

Child:

$this->health;

Error.


Method Overriding

Children may replace parent methods.


Parent:

class Animal {

public function sound() {

echo "Unknown";

}

}

Child:

class Dog
extends Animal {

public function sound() {

echo "Bark";

}

}

Usage:

$dog->sound();

Output:

Bark

Visualization

Animal::sound()

Dog::sound()

Child version overrides parent version.


Parent Keyword

Sometimes child wants parent behavior too.


Example:

class Animal {

public function sound() {

echo "Animal ";

}

}

Child:

class Dog
extends Animal {

public function sound() {

parent::sound();

echo "Bark";

}

}

Output:

Animal Bark

Constructor Inheritance

Constructors are NOT automatically inherited.


Parent:

class Human {

public function __construct() {

echo "Human";

}

}

Child:

class Player
extends Human {

}

Constructor still works.


But if child defines its own constructor:

class Player
extends Human {

public function __construct() {

}

}

Parent constructor will NOT execute automatically.


Correct Way

class Player
extends Human {

public function __construct() {

parent::__construct();

}

}

Constructor Chain

Entity

Human

Player

Initialization flow:

Entity Constructor

Human Constructor

Player Constructor

Real PocketMine Example

PocketMine internally works like this:

Entity

└── Human

└── Player

Player inherits everything from Human.

Human inherits everything from Entity.


Entity Provides

Position
Health
Movement
Effects
FireTicks

Human Adds

Inventory
Armor
Food
Experience

Player Adds

Network Session
Permissions
Commands
Chat

Plugin Example

PluginBase

Main

Your plugin:

class Main
extends PluginBase {

}

Automatically receives:

$this->getServer()
$this->getLogger()
$this->saveDefaultConfig()

Why Inheritance is Powerful

Without inheritance:

You would need to rewrite thousands of lines.

Inheritance provides:

✅ Reusability

✅ Cleaner Code

✅ Less Duplication


Multi-Level Inheritance

Example:

LivingThing

Animal

Dog

PHP supports unlimited levels.


PHP Does NOT Support Multiple Inheritance

Invalid:

class Player
extends Human, Entity {

}

Error.


Only one parent allowed.


Alternative

Use:

Traits
Interfaces

Instanceof Operator

Checks inheritance.


Example:

$player instanceof Human

Output:

true

Example:

$player instanceof Entity

Output:

true

Visualization:

Player

Human

Entity

Player is also Human and Entity.


Method Resolution

PHP checks:

Child

Parent

Grandparent

Example:

$player->teleport();

Search order:

Player

Human

Entity

Final Classes

Classes can prevent inheritance.

Example:

final class Database {

}

Now:

class MyDB
extends Database {

}

Error.


Final Methods

final public function getId() {

}

Children cannot override.


Real Enterprise Example

Controller

BaseController
UserRepository

Repository
ApiException

ValidationException

Inheritance exists everywhere.


Common Beginner Mistakes


Using Inheritance Everywhere

Not every relationship needs inheritance.

Bad:

Player

Inventory

Player is NOT an Inventory.

Use composition.


Remember:

Inheritance means:

IS-A relationship

Composition means:

HAS-A relationship

Example:

Player IS A Human

Good.


Example:

Player HAS AN Inventory

Use composition.


Bad Example

Car

Engine

Car is not an Engine.


Good Example

Car HAS Engine.

Best Practices

✅ Use inheritance only for true relationships.

✅ Prefer composition when possible.

✅ Keep inheritance chains short.

✅ Use protected carefully.


Exercises


Exercise 1

Create:

Animal

Dog

Exercise 2

Create:

Human

Player

Exercise 3

Create:

Entity

Monster

Mini Project

Design:

Entity

├── Human
│ └── Player

└── Monster
├── Zombie
└── Skeleton

Implement methods.


Quiz

Which keyword creates inheritance?

Answer
extends

Can child access private properties?

Answer

No.


Which keyword accesses parent methods?

Answer
parent

Does PHP support multiple inheritance?

Answer

No.


Which operator checks inheritance?

Answer
instanceof

Real PocketMine Hierarchy

Entity

├── Human
│ └── Player

├── Living
├── Projectile
├── ItemEntity
└── Monster

References

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


Next Chapter

➡ Polymorphism

➡ Abstraction

➡ Interfaces

➡ Abstract Classes