PHPPHP18 min read

Inheritance in PHP (Extend Classes Correctly)

Reuse code with inheritance, override methods safely, and understand when not to use inheritance.

Christopher Lane
December 21, 2025
0.0k0

Inheritance allows a class to extend another class.

Example: base class + child class

```php <?php class Employee { public function __construct(public string $name) {}

public function role(): string { return "Employee"; } }

class Manager extends Employee { public function role(): string { return "Manager"; } }

$e = new Employee("David"); $m = new Manager("Sarah");

echo $e->role(); // Employee echo $m->role(); // Manager ?> ```

When inheritance is useful

- you have a strong "is-a" relationship - Manager is an Employee

When to avoid

If you are just sharing code but relationship is not clear, composition (using another class inside) is often better.

> Next: Interfaces, building flexible and testable code.

#PHP#OOP#Intermediate