What are the potential pitfalls of using database operations within setter methods in PHP classes?

Using database operations within setter methods can lead to tight coupling between the database and the class, making the code harder to maintain and test. It can also result in performance issues as each setter call triggers a database operation. To solve this issue, it's better to separate the database operations from the setter methods by creating dedicated methods for database interactions.

class User {
    private $name;
    
    public function setName($name) {
        $this->name = $name;
    }
    
    public function saveToDatabase() {
        // Database operation to save the user data
    }
}