简体   繁体   English

PHP错误报告

[英]php error reporting

Is there a common standard for reporting errors in your class functions? 报告类函数中的错误是否有通用标准?

I've seen some c functions that you pass the error string to, so that if there is an error you can see what it is in that string, You need pointers for that though. 我已经看到了一些将错误字符串传递给的c函数,因此,如果有错误,您可以看到该字符串中的内容,但是您需要为此提供指针。

I'm reluctant to use the return, because sometimes you do need the return for other variables. 我不愿意使用return,因为有时您确实需要其他变量的return。

One thing I was trying is using a standardized return object that has an error key inside, as well as the data. 我正在尝试的一件事是使用标准化的返回对象,该对象内部具有错误密钥以及数据。 This seems to work ok, but when using it with other classes, seems a bit clumsy. 这似乎可以正常工作,但是与其他类一起使用时,似乎有点笨拙。

so here's my latest idea, the class has an internal variable $error and every time a function is called, $error gets cleared, and updated with the error string or array (could even hold more than one error) and when the function completes we can check if the error is empty. 所以这是我的最新想法,该类有一个内部变量$ error,每次调用函数时,都会清除$ error并使用错误字符串或数组(甚至可以包含多个错误)进行更新,并且在函数完成时,可以检查错误是否为空。

here's the problem with that, if I'm using functions within functions, I end up clearing the error. 这就是问题所在,如果我在函数中使用函数,那么最终会清除错误。 I could hold the string inside a different var, and only push it to error once the before return. 我可以将字符串保存在另一个var中,并且仅在返回之前将其推入错误。

any other ideas to do this gracefully and reliably? 还有其他想法可以优雅而可靠地做到这一点吗?

This is an oft-debated topic, and there is no one true answer. 这是一个经常争论的话题,没有一个真正的答案。

But here are four most common patterns that I'm familiar with 但是,这是我所熟悉的四种最常见的模式

  1. Exceptions 例外情况
  2. Multiple return values / Error Codes 多个返回值/错误代码
  3. Error Parameter 错误参数
  4. Error State 错误状态

And a quick example of each 以及每个例子

Exceptions 例外情况

function doSomething()
{
  if ( /* expression */ )
  {
    throw new Exception( 'something went wrong' );  
  }
}

//  Let the registered exception handler do the work
doSomething();

//  Or handle this call specifically
try {
  doSomething();
}
catch ( Exception $e )
{
  echo $e->getMessage();
}

Error Codes 错误代码

function doSomething()
{
  if ( /* expression #1 */ )
  {
    return -1;
  }

  if ( /* expression #2 */ )
  {
    return -2;
  }
  return 1;
}

$return = doSomething();

switch ( $return )
{
  case -1;
    echo 'Error #1 Found';
    break;
  case -2;
    echo 'Error #2 Found';
    break;
  default:
    echo 'Success';
}

Error parameter 错误参数

function doSomething( &$error )
{
  if ( /* expression #1 */ )
  {
    $error = 'Something went wrong';
  }
}

$error = false;

doSomething( $error );

if ( $error )
{
  echo $error;
}

Error State 错误状态

class Foo
{
  private $error = false;
  private $errorMsg;

  public function doSomething()
  {
    if ( /* expression */ )
    {
      $this->setError( 'Something went wrong' );
    }
  }

  public function isError()
  {
    return $this->error;
  }

  public function getErrorMsg()
  {
    return $this->errorMsg;
  }

  private function setError( $message )
  {
    $this->error = true;
    $this->errorMsg = $message;
  }
}

$f = new Foo();
$f->doSomething();

if ( $f->isError() )
{
  echo $f->getErrorMsg();
}

You need to weigh the pros and cons of each before choosing a system (or combination of systems) for your project. 在为项目选择系统(或系统组合)之前,您需要权衡每种方法的利弊。

There isn't a common standard. 没有通用的标准。 Solution depends entirely on your particular needs. 解决方案完全取决于您的特定需求。

Consider learning and using exceptions . 考虑学习和使用异常 They are not universal solution, but if used properly will go a long way to make your code more readable and understandable. 它们不是通用的解决方案,但是如果使用得当,将会大大提高代码的可读性和可理解性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM