How can the use of the 'new' keyword within a PHP class be optimized for better code structure?

To optimize the use of the 'new' keyword within a PHP class for better code structure, you can implement dependency injection. This involves passing instances of required objects or dependencies into the class constructor rather than creating them within the class using 'new'. This approach promotes better code reusability, testability, and flexibility.

class Database {
    public function __construct($host, $username, $password) {
        // Database connection setup
    }
}

class UserRepository {
    private $db;

    public function __construct(Database $db) {
        $this->db = $db;
    }
}

// Usage
$db = new Database('localhost', 'root', 'password');
$userRepository = new UserRepository($db);