0PricingLogin
PHP Academy · Lesson

Ports and Adapters Explained

Isolate the core with ports and pluggable adapters.

The Hexagon Idea

Ports & Adapters — Alistair Cockburn's Hexagonal Architecture — draws your application as a hexagon. Inside is pure business logic. Every interaction with the outside world (HTTP, DB, queue, clock, email) crosses a port, and each port is satisfied by one or more adapters. The shape has no privileged top or bottom: the UI and the database are symmetric, both just adapters.

Ports Are Interfaces

A port is an interface owned by the application core that expresses a need or capability in domain terms. It must not leak infrastructure vocabulary — no PDOStatement, no GuzzleResponse, no Eloquent.

<?php
// Driven (outbound) port: the core needs to persist users
interface UserRepository
{
    public function byId(UserId $id): ?User;
    public function save(User $user): void;
}

All lessons in this course

  1. From Layered to Clean Architecture
  2. Ports and Adapters Explained
  3. Use Cases and Application Services
  4. Dependency Inversion in Practice
← Back to PHP Academy