The feature illustrated below operates on PHP values, objects, or language metadata.
What Is OOP Composition vs Inheritance in PHP?
Interfaces define contracts, inheritance specializes related types, and composition delegates responsibilities to collaborators.
Example 1: Understanding the Basic Behavior
<?php
interface Reporter { public function report(): string; }
class JsonReporter implements Reporter { public function report(): string { return '{"ok":true}'; } }
echo (new JsonReporter())->report();
Output:
{"ok":true}
Explanation: Interfaces define contracts, inheritance specializes related types, and composition delegates responsibilities to collaborators.
Example 2: Testing Another Case
<?php
class Logger { public function write(string $message): void { echo $message; } }
class Worker { public function __construct(private Logger $logger) {} public function run(): void { $this->logger->write('working'); } }
(new Worker(new Logger()))->run();
Output:
working
Explanation: Composition delegates behavior to collaborators, while inheritance establishes an is-a relation to a parent type.
Practical Example: Applying the Operation
<?php
abstract class Payment { abstract public function label(): string; }
class CardPayment extends Payment { public function label(): string { return 'Card'; } }
echo (new CardPayment())->label();
Output:
Card
Explanation: Extending a parent solely for implementation reuse can create fragile coupling; prefer injecting a collaborator when behavior varies independently.
Important Behavior and Edge Cases
- Composition delegates behavior to collaborators, while inheritance establishes an is-a relation to a parent type.
- Extending a parent solely for implementation reuse can create fragile coupling; prefer injecting a collaborator when behavior varies independently.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
OOP Composition vs Inheritance | Interfaces define contracts, inheritance specializes related types, and composition delegates responsibilities to collaborators. |
Inheritance | Inheritance defines is-a type relationships; composition combines components through has-a relationships. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Misinterpreting the documented behavior of OOP Composition vs Inheritance() for the supplied value. | Composition delegates behavior to collaborators, while inheritance establishes an is-a relation to a parent type. |
| Choosing Inheritance without checking whether it provides the same result. | Inheritance defines is-a type relationships; composition combines components through has-a relationships. |
Key Takeaways
- Interfaces define contracts, inheritance specializes related types, and composition delegates responsibilities to collaborators.
- Composition delegates behavior to collaborators, while inheritance establishes an is-a relation to a parent type.
- Extending a parent solely for implementation reuse can create fragile coupling; prefer injecting a collaborator when behavior varies independently.
The examples demonstrate where OOP Composition vs Inheritance fits within php programming. Verify the relevant preconditions whenever you adapt the code to real data.