How can setter and getter methods in PHP be implemented for easier access to class properties?

Setter and getter methods in PHP can be implemented to provide controlled access to class properties. Setter methods allow you to set the value of a property while getter methods allow you to retrieve the value of a property. This helps in encapsulation and ensures that properties are accessed and modified in a controlled manner.

class Person {
    private $name;

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

$person = new Person();
$person->setName('John Doe');
echo $person->getName(); // Output: John Doe