How can JOIN statements in SQL be effectively used to retrieve data from multiple tables while maintaining object-oriented principles in PHP?

When using JOIN statements in SQL to retrieve data from multiple tables in PHP, it's important to maintain object-oriented principles by separating concerns and keeping code organized. One way to achieve this is by creating separate classes for database operations and data manipulation, allowing for better code reusability and maintainability.

class Database {
    private $connection;

    public function __construct($host, $username, $password, $database) {
        $this->connection = new mysqli($host, $username, $password, $database);
    }

    public function getDataFromMultipleTables() {
        $query = "SELECT * FROM table1 
                  JOIN table2 ON table1.id = table2.table1_id";

        $result = $this->connection->query($query);

        // Process the result and return data
        return $result->fetch_all(MYSQLI_ASSOC);
    }
}

$db = new Database('localhost', 'username', 'password', 'database');
$data = $db->getDataFromMultipleTables();

// Use the retrieved data as needed
foreach ($data as $row) {
    // Do something with each row
}