What is the purpose of using __get() in a PHP class for variable access in views?

Using __get() in a PHP class for variable access in views allows you to control how properties are accessed within the class. This can be useful for implementing getter methods or dynamically retrieving data from the class. It also helps in maintaining encapsulation and controlling access to class properties.

class Example {
    private $data = array();

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

    public function __set($name, $value) {
        $this->data[$name] = $value;
    }
}

$example = new Example();
$example->name = 'John Doe';
echo $example->name; // Output: John Doe