๐ŸŽ New User? Get 20% off your first purchase with code NEWUSER20 ยท โšก Instant download ยท ๐Ÿ”’ Secure checkout Register Now โ†’
Menu

Categories

Programming Concepts Intermediate

What is Composition over Inheritance?

A design principle favoring object composition (has-a relationships) over class inheritance (is-a relationships) for code reuse.

Composition over inheritance advises building complex behavior by combining simple, focused objects rather than creating deep inheritance hierarchies. Inheritance creates tight coupling between parent and child classes, and deep hierarchies become fragile โ€” changing a base class can break all descendants. Composition is more flexible: behaviors can be mixed and matched at runtime, components can be replaced independently, and testing is simpler (mock individual components). In PHP, traits provide a limited form of composition within the class system. The Strategy pattern exemplifies composition โ€” injecting different algorithm implementations rather than subclassing for each variation.

Related Terms

Memoization
An optimization technique that caches function results for given inputs, avoiding redundant computations for repeated calls.
DRY (Don't Repeat Yourself)
A software development principle that aims to reduce code duplication by abstracting common patterns into reusable components.
Design Pattern
A reusable solution template for commonly occurring problems in software design.
Immutable Object
An object whose state cannot be modified after creation, providing thread safety and predictable behavior in concurrent systems.
Garbage Collection
An automatic memory management process that identifies and reclaims memory no longer in use by a program.
Object-Oriented Programming (OOP)
A programming paradigm that organizes software design around objects containing data and behavior.
View All Programming Concepts Terms โ†’