In PHP, how can the use of interfaces help in managing dependencies between objects, such as database connections?

Using interfaces in PHP can help manage dependencies between objects by allowing you to define a contract that specifies the methods a class must implement. This allows for more flexibility in swapping out different implementations of a class without affecting the code that depends on it. For example, by creating an interface for database connections, you can easily switch between different database implementations without changing the code that uses the database connection.

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 PostgreSQLDatabase implements DatabaseInterface {
    public function connect() {
        // PostgreSQL connection logic
    }

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

// Usage example
$database = new MySQLDatabase();
$database->connect();
$database->query("SELECT * FROM table");