How can non-static properties in PHP classes affect inheritance and variable sharing?

Non-static properties in PHP classes can lead to unexpected behavior when dealing with inheritance and variable sharing. To avoid this issue, it is recommended to use static properties or methods within classes to ensure consistent behavior and prevent unintended variable sharing between instances.

class ParentClass {
    protected static $sharedProperty = 'shared';

    public function getSharedProperty() {
        return static::$sharedProperty;
    }
}

class ChildClass extends ParentClass {
    // Inherits the static property from ParentClass
}

$parent = new ParentClass();
$child = new ChildClass();

echo $parent->getSharedProperty(); // Output: shared
echo $child->getSharedProperty(); // Output: shared