Separation Of Concerns¶
Separation of concerns is a design principle that advocates dividing a software system into distinct sections, each addressing a distinct concern. A concern is any piece of interest or focus in a program.
The principle helps keep each section focused on one specific responsibility, making the code easier to understand, maintain, and test. It is closely related to the Single Responsibility Principle, SRP, from SOLID, but applies more broadly at the architectural level as well as at the code level.
Classic examples include separating business logic from presentation, separating configuration from code, and separating I/O from computation.
Violations of this principle often manifest as God Classes or functions that mix unrelated responsibilities, leading to tight coupling and reduced cohesion.
<?php
// Concerns mixed: business logic and output together
function getUserAndDisplay(int $id): void {
$user = fetchFromDatabase($id); // data access
echo "<h1>" . $user['name'] . "</h1>"; // presentation
}
// Concerns separated
function getUser(int $id): array {
return fetchFromDatabase($id); // data access only
}
function displayUser(array $user): void {
echo "<h1>" . $user['name'] . "</h1>"; // presentation only
}
?>
See also Code Decoupling and Modularization in PHP and On the Criteria To Be Used in Decomposing Systems into Modules.
Related : SOLID, Single Responsability Principle (SRP), Model - View - Controller (MVC), God Object, Clean Architecture, Coupling, Decoupling, Cohesion, Abstraction, Onion Architecture, Aspect-Oriented, Business Logic