从函数返回“错误”的最佳实践

时间:2021-07-11 01:49:06

I have a function:

我有一个功能:

public function CustomerRating() {
     $result = $db->query("...");
     $row = $result->fetch_assoc();

     if($row)
          $output = $row['somefield'];
     } else {
          $output = "error";
     }

     return $output;
}

//somewhere on another page...
if(is_numeric($class->CustomerRating()) {
     echo $class->CustomerRating;
} else {
      echo "There is an error with this rating.";
}

Is there a better way to find errors? In this function, if no rows are returned, it doesn't mean an "error" per se, it simply means the value can't be calculated. When I check for the result of a function, I feel like there is a better way to check the data being returned before I display it in the if function. What's the best way to do this? I'd like to return a "false", but how would I check for that when calling the function? Thanks!

有没有更好的方法来查找错误?在这个函数中,如果没有返回任何行,它本身并不意味着“错误”,它只是意味着无法计算该值。当我检查函数的结果时,我觉得有一种更好的方法来检查在if函数中显示它之前返回的数据。最好的方法是什么?我想返回一个“假”,但是在调用函数时我该如何检查?谢谢!

6 个解决方案

#1


8  

There are (in my opinion) 2 common ways:

(在我看来)有两种常见方式:

  1. Returning false
    Many builtin PHP functions do that

    返回false许多内置的PHP函数都是这样做的

  2. Using SPL exceptions
    Evolved PHP frameworks (Symfony2, ZF2, ...) do that

    使用SPL异常演化的PHP框架(Symfony2,ZF2,...)可以做到这一点

#2


3  

Use exceptions. Avoid returning errors from functions and methods

使用例外。避免从函数和方法返回错误

#3


2  

You need exceptions:

你需要例外:

public function CustomerRating() {
     $result = $db->query("...");
     $row = $result->fetch_assoc();
     if ($row !== null) {
          return $row['somefield'];
     } else {
          throw new Exception('There is an error with this rating.');
     }
}

// Somewhere on another page...
try {
    echo $class->CustomerRating();
} catch (Exception $e) {
    echo $e->getMessage();
}

#4


0  

I would use exceptions - Saves on the confusion.

我会使用例外 - 节省混乱。

#5


0  

the best way to deal with errors is to throw an exception. that way you can have all kinds of different errors and handle them accordingly.

处理错误的最佳方法是抛出异常。这样你就可以得到各种不同的错误并相应地处理它们。

you can then just do:

你可以这样做:

try {
    $myvar = CustomerRating();
    //do something with it
} catch (Exception $e) {
    echo $e->getMessage();
}

#6


0  

Try this out:

试试这个:

public function CustomerRating() {
     $result = $db->query("...");
     $row = $result->fetch_assoc();

     if($row){
         $output = $row['somefield'];
     } else {
         $output = false;
     }

     return $output;
}

//somewhere on another page...
if($class->CustomerRating() !== false) {
     echo $class->CustomerRating();
} else {
     echo "There is an error with this rating.";
}

This will make sure that it won't break if you return a zero.

如果你返回零,这将确保它不会中断。

#1


8  

There are (in my opinion) 2 common ways:

(在我看来)有两种常见方式:

  1. Returning false
    Many builtin PHP functions do that

    返回false许多内置的PHP函数都是这样做的

  2. Using SPL exceptions
    Evolved PHP frameworks (Symfony2, ZF2, ...) do that

    使用SPL异常演化的PHP框架(Symfony2,ZF2,...)可以做到这一点

#2


3  

Use exceptions. Avoid returning errors from functions and methods

使用例外。避免从函数和方法返回错误

#3


2  

You need exceptions:

你需要例外:

public function CustomerRating() {
     $result = $db->query("...");
     $row = $result->fetch_assoc();
     if ($row !== null) {
          return $row['somefield'];
     } else {
          throw new Exception('There is an error with this rating.');
     }
}

// Somewhere on another page...
try {
    echo $class->CustomerRating();
} catch (Exception $e) {
    echo $e->getMessage();
}

#4


0  

I would use exceptions - Saves on the confusion.

我会使用例外 - 节省混乱。

#5


0  

the best way to deal with errors is to throw an exception. that way you can have all kinds of different errors and handle them accordingly.

处理错误的最佳方法是抛出异常。这样你就可以得到各种不同的错误并相应地处理它们。

you can then just do:

你可以这样做:

try {
    $myvar = CustomerRating();
    //do something with it
} catch (Exception $e) {
    echo $e->getMessage();
}

#6


0  

Try this out:

试试这个:

public function CustomerRating() {
     $result = $db->query("...");
     $row = $result->fetch_assoc();

     if($row){
         $output = $row['somefield'];
     } else {
         $output = false;
     }

     return $output;
}

//somewhere on another page...
if($class->CustomerRating() !== false) {
     echo $class->CustomerRating();
} else {
     echo "There is an error with this rating.";
}

This will make sure that it won't break if you return a zero.

如果你返回零,这将确保它不会中断。