0

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

ADyson
  • 57,178
  • 14
  • 51
  • 63
jqlifice
  • 13
  • 2
  • Since the function doesn't use `$this`, shouldn't it be `static`? – Barmar Jun 22 '23 at 07:50
  • Method `postgresFatalWarningErrorHandler` seems to be part of a class. See: [Callbacks / Callables](https://www.php.net/manual/en/language.types.callable.php) on now to use a class method as a callback. It uses an array. – KIKO Software Jun 22 '23 at 07:51
  • @barmar I don't know much about best practise php, all I know is that it worked like this b4 I started initializing the dbConnection in the constructor of the utils class. adding the static keyword, still results in the same scenario, ty for your input though! – jqlifice Jun 22 '23 at 07:53
  • @KIKOSoftware Ty very much! That solved the issue! – jqlifice Jun 22 '23 at 07:56

1 Answers1

0

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); });
Barmar
  • 741,623
  • 53
  • 500
  • 612