如何在PHP中设计错误报告

时间:2022-05-11 16:56:02

How should I write error reporting modules in PHP? Say, I want to write a function in PHP: 'bool isDuplicateEmail($email)'. In that function, I want to check if the $email is already present in the database. It will return 'true', if exists. Else 'false'.

我应该如何在PHP中编写错误报告模块?说,我想用PHP编写一个函数:'bool isDuplicateEmail($ email)'。在该函数中,我想检查$ email是否已存在于数据库中。如果存在,它将返回'true'。否则'假'。

Now, the query execution can also fail, In that time I want to report 'Internal Error' to the user.

现在,查询执行也可能失败,那时我想向用户报告“内部错误”。

The function should not die with typical mysql error: die(mysql_error(). My web app has two interfaces: browser and email(You can perform certain actions by sending an email). In both cases it should report error in good aesthetic. Do I really have to use exception handling for this?

该函数不应该死于典型的mysql错误:die(mysql_error()。我的Web应用程序有两个接口:浏览器和电子邮件(您可以通过发送电子邮件执行某些操作)。在这两种情况下,它应报告错误的良好美学。我真的必须使用异常处理吗?

Can anyone point me to some good PHP project where I can learn how to design robust PHP web-app?

谁能指点我一些好的PHP项目,在那里我可以学习如何设计健壮的PHP网络应用程序?

4 个解决方案

#1


In my PHP projects, I have tried several different tacts. I've come to the following solution which seems to work well for me:

在我的PHP项目中,我尝试了几种不同的技巧。我来到以下解决方案似乎对我有用:

First, any major PHP application I write has some sort of central singleton that manages application-level data and behaviors. The "Application" object. I mention that here because I use this object to collect generated feedback from every other module. The rendering module can query the application object for the feedback it deems should be displayed to the user.

首先,我编写的任何主要PHP应用程序都有某种管理应用程序级数据和行为的*单例。 “应用程序”对象。我在这里提到,因为我使用此对象来收集来自其他每个模块的生成反馈。呈现模块可以向应用程序对象查询它认为应该向用户显示的反馈。

On a lower-level, every class is derived from some base class that contains error management methods. For example an "AddError(code,string,global)" and "GetErrors()" and "ClearErrors". The "AddError" method does two things: stores a local copy of that error in an instance-specific array for that object and (optionally) notifies the application object of this error ("global" is a boolean) which then stores that error for future use in rendering.

在较低级别,每个类都派生自一些包含错误管理方法的基类。例如“AddError(code,string,global)”和“GetErrors()”和“ClearErrors”。 “AddError”方法执行两项操作:将该错误的本地副本存储在该对象的特定于实例的数组中,并(可选)通知应用程序对象此错误(“global”是一个布尔值),然后存储该错误未来在渲染中的用途。

So now here's how it works in practice:

所以现在这是它在实践中的运作方式:

Note that 'Object' defines the following methods: AddError ClearErrors GetErrorCodes GetErrorsAsStrings GetErrorCount and maybe HasError for convenience

请注意,'Object'定义了以下方法:AddError ClearErrors GetErrorCodes GetErrorsAsStrings GetErrorCount,也许HasError为方便起见

// $GLOBALS['app'] = new Application();

class MyObject extends Object
{
     /**
      * @return bool Returns false if failed
      */
     public function DoThing()
     {
          $this->ClearErrors();
          if ([something succeeded])
          {
              return true;
          }
          else 
          {
              $this->AddError(ERR_OP_FAILED,"Thing could not be done");
              return false;
          }              
     }
}

$ob = new MyObject();
if ($ob->DoThing()) 
{
   echo 'Success.';
}
else 
{
   // Right now, i may not really care *why* it didn't work (the user
   // may want to know about the problem, though (see below).
   $ob->TrySomethingElse();
}

// ...LATER ON IN THE RENDERING MODULE
echo implode('<br/>',$GLOBALS['app']->GetErrorsAsStrings());

The reason I like this is because:

我喜欢这个的原因是因为:

  1. I hate exceptions because I personally believe they make code more convoluted that it needs to be
  2. 我讨厌异常,因为我个人认为它们使代码更加复杂

  3. Sometimes you just need to know that a function succeeded or failed and not exactly what went wrong
  4. 有时您只需要知道某个功能是成功还是失败,而不是确切地说出了什么问题

  5. A lot of times you don't need a specific error code but you need a specific error string and you don't want to create an error code for every single possible error condition. Sometimes you really just want to use an "opfailed" code but go into some detail for the user's sake in the string itself. This allows for that flexibility
  6. 很多时候,您不需要特定的错误代码,但需要特定的错误字符串,并且您不希望为每个可能的错误条件创建错误代码。有时你真的只想使用“opfailed”代码,但为了用户的缘故在字符串本身中进行一些细节。这允许这种灵活性

  7. Having two error collection locations (the local level for use by the calling algorithm and global level for use by rendering modules for telling the user about them) has really worked for me to give each functional area exactly what it needs to get things done.
  8. 有两个错误收集位置(调用算法使用的本地级别和渲染模块用于告诉用户有关它们的全局级别)确实让我能够为每个功能区域提供完成工作所需的功能。

#2


Using MVC, i always use some sort of default error/exception handler, where actions with exceptions (and no own error-/exceptionhandling) will be caught.
There you could decide to answer via email or browser-response, and it will always have the same look :)

使用MVC,我总是使用某种默认错误/异常处理程序,其中将捕获具有异常(并且没有自己的错误/异常处理)的操作。在那里你可以决定通过电子邮件或浏览器响应回答,它总是有相同的外观:)

#3


I'd use a framework like Zend Framework that has a thorough exception handling mechanism built all through it.

我使用像Zend Framework这样的框架,它具有完整的异常处理机制。

#4


Look into exception handling and error handling in the php manual. Also read the comments at the bottom, very useful.

查看php手册中的异常处理和错误处理。另请阅读底部的评论,非常有用。

There's aslo a method explained in those page how to convert PHP errors into exceptions, so you only deal with exceptions (for the most part).

还有一个方法在那些页面中解释了如何将PHP错误转换为异常,因此您只处理异常(大多数情况下)。

#1


In my PHP projects, I have tried several different tacts. I've come to the following solution which seems to work well for me:

在我的PHP项目中,我尝试了几种不同的技巧。我来到以下解决方案似乎对我有用:

First, any major PHP application I write has some sort of central singleton that manages application-level data and behaviors. The "Application" object. I mention that here because I use this object to collect generated feedback from every other module. The rendering module can query the application object for the feedback it deems should be displayed to the user.

首先,我编写的任何主要PHP应用程序都有某种管理应用程序级数据和行为的*单例。 “应用程序”对象。我在这里提到,因为我使用此对象来收集来自其他每个模块的生成反馈。呈现模块可以向应用程序对象查询它认为应该向用户显示的反馈。

On a lower-level, every class is derived from some base class that contains error management methods. For example an "AddError(code,string,global)" and "GetErrors()" and "ClearErrors". The "AddError" method does two things: stores a local copy of that error in an instance-specific array for that object and (optionally) notifies the application object of this error ("global" is a boolean) which then stores that error for future use in rendering.

在较低级别,每个类都派生自一些包含错误管理方法的基类。例如“AddError(code,string,global)”和“GetErrors()”和“ClearErrors”。 “AddError”方法执行两项操作:将该错误的本地副本存储在该对象的特定于实例的数组中,并(可选)通知应用程序对象此错误(“global”是一个布尔值),然后存储该错误未来在渲染中的用途。

So now here's how it works in practice:

所以现在这是它在实践中的运作方式:

Note that 'Object' defines the following methods: AddError ClearErrors GetErrorCodes GetErrorsAsStrings GetErrorCount and maybe HasError for convenience

请注意,'Object'定义了以下方法:AddError ClearErrors GetErrorCodes GetErrorsAsStrings GetErrorCount,也许HasError为方便起见

// $GLOBALS['app'] = new Application();

class MyObject extends Object
{
     /**
      * @return bool Returns false if failed
      */
     public function DoThing()
     {
          $this->ClearErrors();
          if ([something succeeded])
          {
              return true;
          }
          else 
          {
              $this->AddError(ERR_OP_FAILED,"Thing could not be done");
              return false;
          }              
     }
}

$ob = new MyObject();
if ($ob->DoThing()) 
{
   echo 'Success.';
}
else 
{
   // Right now, i may not really care *why* it didn't work (the user
   // may want to know about the problem, though (see below).
   $ob->TrySomethingElse();
}

// ...LATER ON IN THE RENDERING MODULE
echo implode('<br/>',$GLOBALS['app']->GetErrorsAsStrings());

The reason I like this is because:

我喜欢这个的原因是因为:

  1. I hate exceptions because I personally believe they make code more convoluted that it needs to be
  2. 我讨厌异常,因为我个人认为它们使代码更加复杂

  3. Sometimes you just need to know that a function succeeded or failed and not exactly what went wrong
  4. 有时您只需要知道某个功能是成功还是失败,而不是确切地说出了什么问题

  5. A lot of times you don't need a specific error code but you need a specific error string and you don't want to create an error code for every single possible error condition. Sometimes you really just want to use an "opfailed" code but go into some detail for the user's sake in the string itself. This allows for that flexibility
  6. 很多时候,您不需要特定的错误代码,但需要特定的错误字符串,并且您不希望为每个可能的错误条件创建错误代码。有时你真的只想使用“opfailed”代码,但为了用户的缘故在字符串本身中进行一些细节。这允许这种灵活性

  7. Having two error collection locations (the local level for use by the calling algorithm and global level for use by rendering modules for telling the user about them) has really worked for me to give each functional area exactly what it needs to get things done.
  8. 有两个错误收集位置(调用算法使用的本地级别和渲染模块用于告诉用户有关它们的全局级别)确实让我能够为每个功能区域提供完成工作所需的功能。

#2


Using MVC, i always use some sort of default error/exception handler, where actions with exceptions (and no own error-/exceptionhandling) will be caught.
There you could decide to answer via email or browser-response, and it will always have the same look :)

使用MVC,我总是使用某种默认错误/异常处理程序,其中将捕获具有异常(并且没有自己的错误/异常处理)的操作。在那里你可以决定通过电子邮件或浏览器响应回答,它总是有相同的外观:)

#3


I'd use a framework like Zend Framework that has a thorough exception handling mechanism built all through it.

我使用像Zend Framework这样的框架,它具有完整的异常处理机制。

#4


Look into exception handling and error handling in the php manual. Also read the comments at the bottom, very useful.

查看php手册中的异常处理和错误处理。另请阅读底部的评论,非常有用。

There's aslo a method explained in those page how to convert PHP errors into exceptions, so you only deal with exceptions (for the most part).

还有一个方法在那些页面中解释了如何将PHP错误转换为异常,因此您只处理异常(大多数情况下)。