In the context of PHP object-oriented programming, how can the separation of concerns be maintained between different classes, especially when dealing with complex data structures and relationships?

To maintain separation of concerns between different classes in PHP object-oriented programming, it is important to clearly define the responsibilities of each class and ensure that they are not overly dependent on each other. One way to achieve this is by using interfaces or abstract classes to define common behavior and ensure consistency in how classes interact with each other. Additionally, utilizing design patterns such as the Factory pattern or Dependency Injection can help manage complex data structures and relationships between classes.

<?php

interface DatabaseInterface {
    public function connect();
    public function query($sql);
}

class MySQLDatabase implements DatabaseInterface {
    public function connect() {
        // MySQL connection logic
    }

    public function query($sql) {
        // MySQL query logic
    }
}

class UserRepository {
    private $database;

    public function __construct(DatabaseInterface $database) {
        $this->database = $database;
    }

    public function getUserById($id) {
        $sql = "SELECT * FROM users WHERE id = $id";
        return $this->database->query($sql);
    }
}

$mysqlDatabase = new MySQLDatabase();
$userRepository = new UserRepository($mysqlDatabase);
$user = $userRepository->getUserById(1);

?>