How can PHP developers optimize their use of FETCH_OBJ and __set methods when implementing Active-Record patterns to improve data handling and object manipulation efficiency?

When implementing Active-Record patterns in PHP, developers can optimize their use of FETCH_OBJ and __set methods by utilizing them efficiently to improve data handling and object manipulation. By properly setting up the __set method to assign values to object properties dynamically, developers can streamline the process of interacting with database records and updating object properties.

class User extends ActiveRecord {
    public function __set($name, $value) {
        $this->$name = $value;
    }
    
    public static function findById($id) {
        $query = "SELECT * FROM users WHERE id = :id";
        $stmt = $db->prepare($query);
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch(PDO::FETCH_OBJ);
        
        if ($result) {
            $user = new User();
            foreach ($result as $key => $value) {
                $user->$key = $value;
            }
            return $user;
        }
        
        return null;
    }
}