Time Is a Dependency: Injecting a Clock Into Your PHP Domain

📰 Dev.to · Gabriel Anhaia

Learn to inject a clock into your PHP domain to make time testable and avoid hidden dependencies

intermediate Published 13 Jun 2026
Action Steps
  1. Identify hidden dependencies in your PHP code
  2. Create a Clock port to define the interface for time-related operations
  3. Implement a SystemClock adapter to provide the current time
  4. Use a FrozenClock to freeze time for testing purposes
  5. Inject the Clock into your domain to make time testable
Who Needs to Know This

Developers and software engineers can benefit from this approach to improve the testability and maintainability of their PHP applications

Key Insight

💡 Time can be a hidden dependency in your code, but injecting a Clock makes it testable and more maintainable

Share This
💡 Make time testable in your PHP domain by injecting a Clock!

Key Takeaways

Learn to inject a clock into your PHP domain to make time testable and avoid hidden dependencies

Full Article

new DateTimeImmutable() in your domain is a hidden dependency. A Clock port, a SystemClock adapter, and a FrozenClock make time testable.
Read full article → ← Back to Reads