最好的做法是使用PHP异常处理 [英] The best practise when to use PHP exception handling

查看:130
本文介绍了最好的做法是使用PHP异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题在多次之前被问过,但是他们似乎有一个桌面应用程序的背景,而不是Web应用程序。我正在使用PHP构建一个Web应用程序。我将使用控制器和模型文件来演示我的问题。

I know this question has been asked before numorous times but they seem to have a desktop application background and not web application. I am currently building a web application using PHP. I'm going to use a controller and model file to demonstrate my question.

控制器文件

此文件请求数据库查询和获取数据

This file calls upon the database to query and fetch data

<?php public function index() {
    $this->database->query("SELECT user_name, FROM test WHERE user_name = :user_name");
    $this->database->execute_query("jim");
    $this->view->data = $this->database->fetch_query();
?>

数据库文件

<?php

class DB {

    private $datasourcename;
    private $user;
    private $password;
    private $connection;
    private $prepare;
    private $query;

    function __construct($dsn, $user, $password) {

        $this->datasourcename = $dsn;
        $this->user = $user;
        $this->password = $password;    

        $this->connection = new PDO($this->datasourcename, $this->user, $this->password);
    }

    public function query($query) {

        $this->query = $query;
        try {
            if (empty($query)) {

                throw new Exception("The query is empty");
                return false;
            }
        } catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "<br/>";
        }
        if (strstr($query, ":") == FALSE) {
            return $this->connection->query($query);
        } else {

            $this->prepare = $this->connection->prepare($query);
        }
    }

    public function execute_query($valarg = array()) {

        try {
            if (empty($valarg)) {

                throw new Exception("There are no values in the execute query function");
                return false;
            }
            if (is_array($valarg) == false) {

                throw new Exception("The values inserted are not in an array");
                return false;
            } else {

                $query = $this->query;
                $paramkeys = array();
                $paramArr = array();

                if (strstr($query, ":")) {    

                    preg_match_all("/:(\w+)/", $query, $paramkeys);

                    $paramArr = array_combine($paramkeys[0], $valarg);

                    $this->prepare->execute($paramArr);
                }
            }
        } catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "<br/>";
        }
    }

    public function fetch_query() {

        try {
            if($this->execute_query() == false)    {

                 throw new Exception("Sorry you need to fix this first");
            }

             if($this->query() == false)    {

                 throw new Exception("Sorry you need to fix this first");
            }
            else    {

                 $result = $this->prepare->fetch(PDO::FETCH_ASSOC);

                return $result;
            }
        }
        catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "<br/>";
        }
    }
}

?>

我在我的数据库类中使用异常处理,作为开发人员使用由该类正确地为了应用程序的工作,我是否正确地认为这是使用它们的最佳做法?是否有明确的切割最佳实践什么时候和如何使用它们?我在这个网站上看过类似的问题,但背景是台式机应用设计,而不是Web应用程序。

I have used exception handling in my database class as a way for a developer to use the functions provided by that class correctly in order for the application to work, am I right im thinking that this is the best practise to use them? Is there a clear cut best practise when and how to use them? I have read similar questions on this site but the background was desktop application design and not web application.

推荐答案

Web编程没有什么不同比在这方面的桌面编程。

Web programming is no different than desktop programming in this regard.

使用异常异常事件。对于任何预期的使用返回值。

Use exceptions for exceptional events. Use return values for anything expected.

我认为看到一个空的查询结果不会抛出一个异常。我不会返回任何数据。如果数据库查询失败了,这将是抛出异常的一个原因。此外,您的异常消息应该更具描述性,而不是您需要解决此问题。

I think seeing an empty query result isn't something that will have an exception thrown, generally. I'd just return no data. If the database query failed somehow, that would be a reason to throw an exception. Also, your exception messages should probably be more descriptive, rather than "you need to fix this".

最后,在PHP 5.3中,可以通过传递设置内部异常它作为异常构造函数中的第三个参数。

Finally, in PHP 5.3, you can set an inner exception by passing it as the third parameter in the Exception constructor.

http://www.php.net/manual/en/class.exception.php

这篇关于最好的做法是使用PHP异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆