dongwu8064 2012-03-27 21:35
浏览 25
已采纳

何时使用PHP异常处理的最佳实践

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.

Controller file

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();
?>

The database file

<?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/>";
        }
    }
}

?>

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.

  • 写回答

1条回答 默认 最新

  • du3932066 2012-03-27 21:37
    关注

    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".

    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

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

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!