Aspect-Oriented¶
Aspect-oriented design structures code so that cross-cutting concerns: behaviors that affect multiple classes or layers without belonging to any single one, are isolated into dedicated units called aspects.
In applications, common cross-cutting concerns include logging, caching, authorization, validation, and transaction management. They are typically implemented through decorators, middleware chains, event listeners, or AOP frameworks.
The key principle is that business logic classes should remain unaware of these concerns: they are woven in by the surrounding infrastructure rather than being called explicitly. This improves cohesion, reduces duplication, and makes each concern independently testable and replaceable.
<?php
// Cross-cutting concern isolated as a decorator
final class LoggingRepository implements UserRepositoryInterface {
public function __construct(
private readonly UserRepositoryInterface $inner,
private readonly LoggerInterface $logger,
) {}
public function find(int $id): User {
$this->logger->info('find', ['id' => $id]);
return $this->inner->find($id);
}
}
?>
See also Go! AOP Framework for PHP.
Related : Aspect Oriented Programming, Decorator Pattern, Middleware Pattern, Proxy, Paradigm, Separation Of Concerns, Decoupling
Related packages : goaop/framework