Composition injects collaborators into a class; inheritance extends a parent implementation when a genuine is-a relationship exists.
The examples below begin with the core behavior, then move to an application-oriented use.
How Composition vs Inheritance in PHP Works
Composition injects collaborators into a class; inheritance extends a parent implementation when a genuine is-a relationship exists.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
class Logger { public function write(string $message): void { echo $message; } }
class PaymentService { public function __construct(private Logger $logger) {} public function pay(): void { $this->logger->write("paid"); } }
(new PaymentService(new Logger()))->pay();
Output:
paid
Composition injects collaborators into a class; inheritance extends a parent implementation when a genuine is-a relationship exists.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class Document { public function title(): string { return "Guide"; } }
class PdfDocument extends Document { public function format(): string { return "PDF"; } }
echo (new PdfDocument())->title();
Output:
Guide
Avoid inheriting only to reuse incidental code: changes to the parent can unexpectedly affect subclasses.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
interface Notifier { public function notify(string $message): void; }
class EchoNotifier implements Notifier { public function notify(string $message): void { echo $message; } }
class OrderService { public function __construct(private Notifier $notifier) {} public function complete(): void { $this->notifier->notify("Order complete"); } }
(new OrderService(new EchoNotifier()))->complete();
Output:
Order complete
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Avoid inheriting only to reuse incidental code: changes to the parent can unexpectedly affect subclasses.
- Composition injects collaborators into a class; inheritance extends a parent implementation when a genuine is-a relationship exists.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
Composition vs Inheritance in PHP | Composition injects collaborators into a class; inheritance extends a parent implementation when a genuine is-a relationship exists. |
| Alternative approach | Composition is more flexible for swapping services and writing test doubles. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of Composition vs Inheritance in PHP | Avoid inheriting only to reuse incidental code: changes to the parent can unexpectedly affect subclasses. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Composition is more flexible for swapping services and writing test doubles. |
Key Takeaways
- Composition injects collaborators into a class; inheritance extends a parent implementation when a genuine is-a relationship exists.
- Avoid inheriting only to reuse incidental code: changes to the parent can unexpectedly affect subclasses.
- Use the example that matches the values, runtime version and dependencies in your application.
Composition vs Inheritance in PHP is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.