How can magic methods in PHP be used to handle dynamic variable assignment in a class?

Magic methods in PHP, such as `__set()`, can be used to handle dynamic variable assignment in a class. By implementing the `__set()` magic method, you can intercept attempts to set inaccessible or non-existent properties within a class and handle them accordingly. This allows for more dynamic and flexible handling of property assignments within a class.

class MyClass {
    private $data = [];

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

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

$obj = new MyClass();
$obj->dynamicVar = 'value';
echo $obj->dynamicVar; // Output: value