An interface allows production and fake implementations to satisfy the same contract.
The examples below begin with the core behavior, then move to an application-oriented use.
How Implementing Interfaces for Testable Code Works
An interface allows production and fake implementations to satisfy the same contract.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
interface SmsGateway { public function send(string $text): bool; }
class FakeGateway implements SmsGateway { public function send(string $text): bool { return $text !== ""; } }
var_dump((new FakeGateway())->send("Test"));
Output:
bool(true)
An interface allows production and fake implementations to satisfy the same contract.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
interface ExchangeRateProvider { public function rate(): float; }
class FixedRateProvider implements ExchangeRateProvider { public function rate(): float { return 1.1; } }
function convert(float $amount, ExchangeRateProvider $provider): float { return $amount * $provider->rate(); }
echo convert(100, new FixedRateProvider());
Output:
110
Keep interfaces focused and do not put implementation-specific methods into a contract only one class can fulfill.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
interface Clock { public function today(): string; }
class FixedClock implements Clock { public function today(): string { return "2026-05-01"; } }
class Reminder { public function __construct(private Clock $clock) {} public function message(): string { return $this->clock->today(); } }
echo (new Reminder(new FixedClock()))->message();
Output:
2026-05-01
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Keep interfaces focused and do not put implementation-specific methods into a contract only one class can fulfill.
- An interface allows production and fake implementations to satisfy the same contract.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
Implementing Interfaces for Testable Code | An interface allows production and fake implementations to satisfy the same contract. |
| Alternative approach | Depending on a concrete service makes isolated tests harder than injecting an interface. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of Implementing Interfaces for Testable Code | Keep interfaces focused and do not put implementation-specific methods into a contract only one class can fulfill. |
| 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 | Depending on a concrete service makes isolated tests harder than injecting an interface. |
Key Takeaways
- An interface allows production and fake implementations to satisfy the same contract.
- Keep interfaces focused and do not put implementation-specific methods into a contract only one class can fulfill.
- Use the example that matches the values, runtime version and dependencies in your application.
Implementing Interfaces for Testable Code 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.