What are the best practices for extending abstract classes in PHP to maintain variable encapsulation?

When extending abstract classes in PHP, it is important to maintain variable encapsulation to ensure data integrity and prevent unintended modification of class properties. One way to achieve this is by using protected visibility for class properties in the abstract class and providing getter and setter methods for accessing and modifying these properties in the child classes.

abstract class AbstractClass {
    protected $property;

    public function getProperty() {
        return $this->property;
    }

    public function setProperty($value) {
        $this->property = $value;
    }

    abstract public function doSomething();
}

class ChildClass extends AbstractClass {
    public function doSomething() {
        // Implement functionality here
    }
}

$child = new ChildClass();
$child->setProperty('value');
echo $child->getProperty(); // Output: value