๐ŸŽ 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

SOLID Principles
Five design principles for writing maintainable, flexible object-oriented code: Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion.
API Design
The practice of designing application programming interfaces that are consistent, intuitive, and maintainable for developers to consume.
SOLID Principles Breakdown
Five object-oriented design principles that guide developers in creating maintainable, flexible, and scalable software systems.
Observer Pattern
A design pattern where an object (subject) maintains a list of dependents (observers) that are notified automatically of state changes.
Clean Code
Code that is easy to read, understand, and maintain - following consistent conventions, meaningful naming, and single-responsibility functions.
Hash Table
A data structure that maps keys to values using a hash function, providing average O(1) time complexity for lookups, insertions, and deletions.
View All Programming Concepts Terms โ†’