How can getters be utilized in PHP entities to provide data for form fields in a web application?

In PHP entities, getters can be utilized to provide data for form fields in a web application by retrieving the values of the entity properties and returning them when needed. This allows for easy access to the entity data and seamless integration with form fields without directly accessing the properties.

class User {
    private $id;
    private $username;
    private $email;

    public function getId() {
        return $this->id;
    }

    public function getUsername() {
        return $this->username;
    }

    public function getEmail() {
        return $this->email;
    }
}

$user = new User();
$user->setId(1);
$user->setUsername('john_doe');
$user->setEmail('john.doe@example.com');

// Get data for form fields
$id = $user->getId();
$username = $user->getUsername();
$email = $user->getEmail();