Is saving error reports in a database a suitable solution for error handling in PHP classes?

Saving error reports in a database can be a suitable solution for error handling in PHP classes as it allows for easy tracking and analysis of errors. By storing error information in a database, developers can quickly identify and address recurring issues. Additionally, this approach provides a centralized location for error logs, making it easier to monitor and manage errors in the application.

<?php

class ErrorHandler {
    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function logError($errorMessage) {
        $query = "INSERT INTO error_logs (error_message, created_at) VALUES (:error_message, NOW())";
        $stmt = $this->db->prepare($query);
        $stmt->bindParam(':error_message', $errorMessage);
        $stmt->execute();
    }
}

// Example usage
$db = new PDO('mysql:host=localhost;dbname=error_handling', 'username', 'password');
$errorHandler = new ErrorHandler($db);
$errorHandler->logError("An error occurred in the application");
?>