Senior LevelPHP 8+
How do you structure an enterprise PHP codebase using Hexagonal Architecture (Ports and Adapters) and DDD?
❓ Interview Prompt: Explain Hexagonal Architecture (Ports and Adapters), Domain-Driven Design (DDD), isolating core business logic, and framework decoupling.
Hexagonal Architecture (Ports & Adapters) decouples core domain business logic from external frameworks, databases, and third-party APIs.
The 3 Layers:
1
Core Domain Layer (Center): Contains pure PHP entities, value objects, and domain rules. Zero dependencies on Laravel/Symfony or database packages!
2
Ports (Interfaces): Interfaces defined by the domain layer specifying required capabilities (e.g. 'OrderRepositoryInterface').
3
Adapters (Outer Infrastructure): Implementations of Ports using specific technologies ('DoctrineOrderRepository', 'StripePaymentAdapter'). Frameworks act as outer delivery mechanisms.
Code Implementation Example
php
<?php
namespace DomainRepositories;
// PORT: Interface owned by Domain Layer
interface OrderRepositoryInterface {
public function save(Order $order): void;
}
namespace InfrastructurePersistence;
use DomainRepositoriesOrderRepositoryInterface;
// ADAPTER: Infrastructure implementation using PDO
class PDOOrderRepository implements OrderRepositoryInterface {
public function __construct(private PDO $pdo) {}
public function save(Order $order): void {
// SQL persistence logic...
}
}💡Decoupling domain interfaces (Ports) from database implementations (Adapters).
🎯 Interview Key Takeaway
Hexagonal Architecture isolates pure domain logic in the center, interfacing with infrastructure via Ports (interfaces) and Adapters.
#Hexagonal Architecture#DDD#Ports and Adapters#Architecture#Senior
Back to all PHP 8+ Questions→