How does constructor injection in PHP differ from using setters for object initialization?

Constructor injection in PHP involves passing dependencies to a class through its constructor, ensuring that the object is fully initialized when it is created. This allows for better control over the object's state and promotes better encapsulation. On the other hand, using setters for object initialization involves setting the object's properties after it has been created, which can lead to objects being in an inconsistent state during their lifecycle.

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

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

// Example of constructor injection
$connection = new DatabaseConnection('localhost', 'root', 'password');