phpphp-pgsql

Callback function not found or invalid name, in set_error_handler


The setup works like this: There is a Database Class and a Utils Class

The scripts initialize an instance of the Utils Class which initializes the DatabaseConnection instance (I assume that its got something to do with this, as it worked b4 the utils class initialized it)

In the DatabaseConnection Class there are Functions that might try to insert values that conflict due to their columns expecting UNIQUE values. As this insert, does throw a weird warning, but no proper response code I wrote a simple error handler, that I am trying to initialize b4 running the query:

private function postgresFatalWarningErrorHandler($e, $eStr): void{
    throw new Exception(explode("DETAIL:", $eStr)[1]);
}

(part of the dbConnection class)

set_error_handler("postgresFatalWarningErrorHandler");
$res = pg_execute($this->dbConnection, "query", $dataArray);
restore_error_handler();

(part of any function in the dbConnection class possibly violating the UNIQUE constraint)

Note that this setup worked perfectly b4 I started to let the utils class initialize the DatabaseConnection, which is necessary due to some functions in the utils class, but also some scripts utilizing the utils class needing direct access to functions in the DatabaseConnection.

The following error is thrown:

PHP Fatal error: Uncaught TypeError: set_error_handler(): Argument #1 ($callback) must be a valid callback or null, function "postgresFatalWarningErrorHandler" not found or invalid function name in [path]\dbConnection.php:78


Solution

  • postgresFatalWarningErrorHandler is a class method, not a function. Since it's not a static method, it needs to be called on an instance of the class.

    Try this:

    set_error_handler([$this, "postgresFatalWarningErrorHandler"]);
    

    or

    set_error_handler(function($e, $eStr) { $this->postgresFatalWarningErrorHandler($e, $eStr); });