PHP set_error_handler所有项目

I have a class that have a personal handle error, so I want to set this function to all my project. How can I do that? Is this a good approach?

My class :

<?php 

define("LOG_FILE", "/tmp/errors.log");

class ErrorManager{


function handleError($error_number, $error_messsage, $error_file){
        ini_set("log_errors", 1);
        error_log( "Some Error message!", 3, LOG_FILE);
        return true;
    }
}


?>

I am calling it like this:

function Connect() {
        $this->status = 0;
        $this->host = $_SESSION['dbhost'];
        $this->user = $_SESSION['dbuser'];
        $this->pass = $_SESSION['dbpassword'];
        $this->dbname = $_SESSION['dbnamebase'];
        $this->db = mysqli_connect($this->host, $this->user, $this->pass);

        $old_error_handler = set_error_handler('ErrorManager::handleError', E_ALL);

        if (!$this->db) {
            trigger_error("errou feio", E_USER_ERROR);
        } else {
            $this->status = 1;
            mysqli_select_db($this->db,$this->dbname);
        }
    }

It looks like you put the error handler initialization in your database connection class. If you want your error handler to be available everywhere and as early as possible, you should put it in whatever file is the entry point of your application (index.php, app.php...). That way, you are sure it will always be active and enabled from the beginning.

As this.lau_ said: you should put it in whatever file is the entry point of your application (index.php, app.php...). That way, you are sure it will always be active and enabled from the beginning.