DELAPARRA
Index
Color
NL
Category: Programming paradigms

Inheritance

Explained by Updated

What is Inheritance?

Inheritance: A child class extends a parent class and inherits its properties and methods. AdminUser extends User — it has everything User has, plus admin-specific permissions. Inheritance lets a class extend another class and reuse its properties and methods. An AdminUser that extends User has everything a user has, plus admin-specific behaviour.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Inheritance

How is Inheritance used in practice?

It is a natural fit for real "is a" relationships and for frameworks that give you a base class to build on, such as Laravel's Model or Controller.

What should you keep in mind with Inheritance?

Used heavily, it causes problems. Deep hierarchies are hard to follow, and a change in a parent class can break children in unexpected ways. The common advice is to prefer composition: give a class the helpers it needs as separate objects, rather than inheriting from a chain of parents. Traits and interfaces in PHP offer other ways to share behaviour. Keep inheritance shallow, usually one or two levels.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with I

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.