How can the "undefined variable" notice in PHP be suppressed when using __get() to access non-public properties?

When using the __get() magic method to access non-public properties in PHP, the issue of receiving an "undefined variable" notice can be suppressed by explicitly checking if the property exists before attempting to access it. This can be done using the isset() function within the __get() method.

class MyClass {
    private $data = ['key' => 'value'];

    public function __get($name) {
        if (isset($this->data[$name])) {
            return $this->data[$name];
        }
        return null;
    }
}

$obj = new MyClass();
echo $obj->key; // Output: value
echo $obj->invalidKey; // No notice, Output: null