🎁 New User? Get 20% off your first purchase with code NEWUSER20 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

Binary Search
An efficient search algorithm that finds a target value in a sorted array by repeatedly dividing the search interval in half.
SOLID Principles Breakdown
Five object-oriented design principles that guide developers in creating maintainable, flexible, and scalable software systems.
Immutable Object
An object whose state cannot be modified after creation, providing thread safety and predictable behavior in concurrent systems.
Agile
A software development methodology that emphasizes iterative development, collaboration, and rapid response to change.
Version Control
A system that records changes to files over time, allowing you to recall specific versions, collaborate, and track history.
Thread
The smallest unit of execution within a process, allowing concurrent operations to run within a single program.
View All Programming Concepts Terms →