英文:
Callback function not found or invalid name, in set_error_handler
问题
设置如下:
有一个Database Class和一个Utils Class
脚本初始化Utils Class的一个实例,该实例初始化了DatabaseConnection实例(我认为它与此有关,因为在Utils Class初始化之前它可以正常工作)
在DatabaseConnection Class中有一些函数,它们可能尝试插入由于其列期望唯一值而发生冲突的值。由于这个插入会引发奇怪的警告,但没有适当的响应代码,我编写了一个简单的错误处理程序,我正在尝试在运行查询之前初始化它:
private function postgresFatalWarningErrorHandler($e, $eStr): void{
throw new Exception(explode("DETAIL:", $eStr)[1]);
}
(dbConnection类的一部分)
set_error_handler("postgresFatalWarningErrorHandler");
$res = pg_execute($this->dbConnection, "query", $dataArray);
restore_error_handler();
(dbConnection类中的可能违反唯一约束的任何函数的一部分)
请注意,在我开始让Utils Class初始化DatabaseConnection之前,这个设置完全正常工作,这是由于Utils Class中的一些函数以及利用Utils Class的一些脚本需要直接访问DatabaseConnection中的函数。
以下错误被抛出:
PHP致命错误:Uncaught TypeError:set_error_handler():参数#1($callback)必须是有效的回调或null,函数“postgresFatalWarningErrorHandler”未找到或无效的函数名称在[path]\dbConnection.php:78中
英文:
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
答案1
得分: 0
postgresFatalWarningErrorHandler
是一个类方法,而不是一个函数。因为它不是一个静态方法,所以它需要在类的实例上调用。
尝试这样做:
set_error_handler([$this, "postgresFatalWarningErrorHandler"]);
或者
set_error_handler(function($e, $eStr) { $this->postgresFatalWarningErrorHandler($e, $eStr); });
英文:
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); });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论