An adapter exposes a consistent interface around an incompatible existing dependency.
What It Does
An adapter exposes a consistent interface around an incompatible existing dependency. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.
The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.
Example 1: Basic Behavior
Understand the primary operation and its inputs.
<?php
interface WeatherPort { public function temperature(): int; }
class LegacyWeather { public function getTempC(): int { return 19; } }
class WeatherAdapter implements WeatherPort { public function __construct(private LegacyWeather $legacy) {} public function temperature(): int { return $this->legacy->getTempC(); } }
echo (new WeatherAdapter(new LegacyWeather()))->temperature();
Example 2: Another Case
Check failure behavior or a different execution path.
<?php
interface SenderPort { public function send(string $message): string; }
class FileSender { public function write(string $text): string { return 'wrote: ' . $text; } }
class FileSenderAdapter implements SenderPort { public function __construct(private FileSender $inner) {} public function send(string $message): string { return $this->inner->write($message); } }
echo (new FileSenderAdapter(new FileSender()))->send('hello');
Practical Example: Putting It to Use
Apply the behavior in a small, controlled scenario.
<?php
interface ClockPort { public function now(): int; }
class FixedClock implements ClockPort { public function now(): int { return 123; } }
function timestamp(ClockPort $clock): int { return $clock->now(); }
echo timestamp(new FixedClock());
Important Behavior and Edge Cases
An adapter translates protocols or method names; it should not hide security or error semantics.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| Applying the Adapter Pattern in PHP | An adapter exposes a consistent interface around an incompatible existing dependency. |
| Related approach | Use a decorator when you want to add behavior without changing the underlying interface. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for Applying the Adapter Pattern in PHP | An adapter translates protocols or method names; it should not hide security or error semantics. |
| Assuming a related mechanism has the same behavior | Use a decorator when you want to add behavior without changing the underlying interface. |
Key Takeaways
- An adapter exposes a consistent interface around an incompatible existing dependency.
- An adapter translates protocols or method names; it should not hide security or error semantics.
- Use a decorator when you want to add behavior without changing the underlying interface.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.