How can methods (getters) be used to access protected properties in PHP objects?

To access protected properties in PHP objects, you can create getter methods within the class that return the values of these properties. By using getters, you can control how these protected properties are accessed and ensure that any necessary logic or validation is applied before returning the values.

class MyClass {
    protected $myProperty;

    public function getMyProperty() {
        return $this->myProperty;
    }
}

$obj = new MyClass();
echo $obj->getMyProperty();