How can Dependency Injection be utilized in PHP to handle dynamically initializing classes with varying constructor parameters?

When dealing with dynamically initializing classes with varying constructor parameters in PHP, Dependency Injection can be utilized. By using Dependency Injection, we can pass the necessary dependencies to the class through its constructor, allowing for flexibility in initializing classes with different parameters.

class Database {
    private $host;
    private $username;
    private $password;

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

    // Other methods of the Database class
}

$host = "localhost";
$username = "root";
$password = "password";

$database = new Database($host, $username, $password);