0

我有一个大问题。好吧,我正在尝试保存我的 Web 应用程序中发生的错误。例如(仅作为示例),如果我除以零,或者在查询中发生错误或类似情况。所以,我想抓住它并将其保存在数据库中。但是 CakePHP 的 handleError 是静态的,所以我不能使用 $this->MyModel->saveError(...)。

在我的 /app/Lib/AppError.php 我有这个:

class AppError extends ErrorHandler
{
     public $uses = array('Errors.Error');

     public static function handleError($code, $description, $file = null, $line = null, $context = null)
     {
         //This print the error:
         echo "Code: ".$code." Description: ".$description." File: ".$file." Line: ".$line."<br>";
         //I want to do this, (save to the database):
         $this->Error->saveError($code, $description, $file, $line);

     }
}

没有 $this->Error->saveError($code, $description, $file, $line); 它有效,但我不仅想显示错误。我认为需要一个例子或类似的东西。请帮我。问候和谢谢。谢谢... PD:对不起英语,我是一名英语学生...

4

1 回答 1

0

您将函数定义handelError为静态函数,因此该变量$this不存在,因为静态环境无法知道它所指的对象,只能知道其类名。在静态环境中,self::必须使用,而不是$this,例如

self::Error->saveError($code, $description, $file, $line);

这也意味着Error必须声明该属性static

于 2012-10-03T18:17:31.063 回答