Middle
What is a latch?
sobes.tech AI
Answer from AI
Coupling is the degree of dependence between modules or components of a program. The stronger the coupling, the more tightly connected the modules are, and changes in one can affect the other. In object-oriented programming, the goal is to achieve loose coupling to improve maintainability and flexibility of the code.
For example, if class A directly uses the internal details of class B, this is strong coupling. To reduce coupling, interfaces, abstractions, or dependency injection are used.
An example of loose coupling in PHP:
interface LoggerInterface {
public function log(string $message);
}
class FileLogger implements LoggerInterface {
public function log(string $message) {
// write to file
}
}
class UserService {
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function createUser() {
// create user
$this->logger->log('User created');
}
}
Here, UserService depends on the abstraction LoggerInterface, not on a specific implementation, which reduces coupling.