dpxw17759 2012-10-11 09:05
浏览 14
已采纳

php中错误处理的正确方法

I realise this is a big topic, but I'd appreciate some rough ideas on the best practice ways for handling errors, particularly in OO PHP (but also interested in good patterns which transcend language specifics). Let's assume these classes and applications are large scale ones.

I also realise that the most recommended way is to make use of PHPs Exception model, and I'm interested in knowing how the below example might translate into this.

EDIT: what I'm looking for is ideas on how best to handle say the errors generated when validating say form data (not errors with how the method has been called), perhaps the Exception model isn't for this use case END EDIT

Particularly given that not all errors need to be displayed/used immediately (can multiple exceptions be thown in the same method?), some might need to be logged (that is another question I guess), some might only be relevant if other dependencies are true (or false), and some might just be warnings, some might be critical and some might be statuses/notifications.

The current approach I take is something along the lines of:

class ClassName()
{
    public MethodName( $data, &$errors )
    {
        $e = [];

        // validate content and perform data handling
        // any errors should be added to the array $e

        if( empty( $e ) ) {
            return $processed_data;
        } else {
            $errors = $e;
            return false;
        }
    }
}

// and then calling:
$method_call = ClassName::MethodName( $data, $errors );

if( $method_call ) // do something with the data/display success message etc.
else // decide what to do with any errors

Tar Folks,

  • 写回答

1条回答 默认 最新

  • duanbo7517 2012-10-11 09:12
    关注

    It depends really. The "proper" OO way to do that is to use exceptions though. There are already many different types of exception built in (see http://www.php.net/manual/en/spl.exceptions.php) and you can define your own (see http://php.net/manual/en/language.exceptions.php).

    A way to use them could be similar to this

    function foo($bar)
    {
        if(!is_string($bar))
            throw new InvalidArgumentException("String argument expected");
    
        if(strlen($bar) > 50)
            throw new LengthException("String argument is too long!");
    }
    

    Then, in calling code, use try/catch, e.g.

    try {
         foo("hjsdkfjhkvbnsjd");
    } catch(LengthException $ex) {
         // Trim the string
    } catch (InvalidArgumentException $ex) {
         // Try to recover. Cast or trim or something
    } catch (Exception $ex) {
         // We hit an exception we're not explicitly handling.  
         // Gracefully exit
         die($ex->Message);
    }
    

    In any case, an exception should only be thrown if the method has actually been called wrong, or if the method fails in a fatal way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件