What are the differences between accessing class members directly and using setter/getter methods in PHP?

When accessing class members directly, you are bypassing any validation or logic that may be implemented in setter/getter methods. Using setter/getter methods allows you to control access to class members, enforce validation rules, and easily modify the behavior of the class without affecting external code.

class User {
    private $username;

    public function setUsername($username) {
        // Validation logic can be implemented here
        $this->username = $username;
    }

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

$user = new User();
$user->setUsername('john_doe');
echo $user->getUsername();