PHP5,析构函数中的异常

kxc*_*kxc 5 php destructor exception

我现在正在读"专业人士的PHP5",这是2006年的出版物.在一个例子中,他们在析构函数中抛出异常,很长一段时间我无法理解为什么我的析构函数中的最后一个异常不起作用,然后我搜索它并发现在PHP => 5.3中不可用于在析构函数中抛出异常.那么如何做得更好,如果在析构函数中我更新我的数据库if变量$needsUpdate = true;并关闭我的数据库连接,所以我想在数据库无法更新时抛出异常.例如异常我抛入我的数据库类,但在主文件中捕获它们,如下所示:

它的类Widget:

class Widget {

    private $_id;
    private $_name;
    private $_description; private $_hDB;
    private $_needsUpdating = false;

    public function __construct($widgetID) {

        $this->_hDB = new mysqli('localhost', 'phpbook', 'Un+)J=<AaH', 'phpbook');
        if (! $this->_hDB) {
            throw new Exception('?? ??????? ??????????? ? ???? ??????');
        }
        $sql = "SELECT `name`, `description` FROM widgets WHERE id = '$widgetID'";
        $rs = $this->_hDB->query($sql);

        if (! $rs) {
            throw new Exception("????????? ?????? ??? ?????? ???? ??????");
        }

        if (! $rs->num_rows) {
            throw new Exception("???????? ?????? ? ???? ?????? ???????????");
        }
        $data = $rs->fetch_assoc();
        $this->_id = $widgetID;
        $this->_name = $data['name'];
        $this->_description = $data['description'];

    }

    public function getName() {
        return $this->_name;
    }

    public function getDescription() {
        return $this->_description;
    }

    public function setName($name) {
        $this->_name = $name;
        $this->_needsUpdating = true;
    }

    public function setDescription($description) {
        $this->_description = $description;
        $this->_needsUpdating = true;
    }

    public function __destruct() {
        if (! $this->_needsUpdating) {
            return;
        }

        $sql = 'UPDAT2E `widgets` SET';
        $sql .= ' `name` = "' . $this->_hDB->real_escape_string($this->_name) . '",';
        $sql .= ' `description` = "' . $this->_hDB->real_escape_string($this->_description) . '" ';
        $sql .= 'WHERE id = ' . $this->_id;


        $rs = $this->_hDB->query($sql);
        if (! $rs) {
            throw new Exception("????????? ?????? ??? ?????????? ???? ??????");
        }
        $this->_hDB->close();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是主要文件.

try {
    $widget = new Widget(2);
    echo "Name: " . $widget->getName() . "<br />\n";
    echo "Description: " . $widget->getDescription() . "<br />\n";
    $widget->setName("Iphone 4 / 64GB");
    $widget->setDescription("New Phone, black color, blablabla");

} catch (Exception $e) {
    die("An error has occurred: " . $e->getMessage());
}
Run Code Online (Sandbox Code Playgroud)

最后一个例外__destruct()没有奏效.

如果更新数据库失败,有没有一种很好的方法来抛出异常?还是有其他正确的方法,我不明白的东西?

Mar*_*ler 3

析构函数中的异常可能会导致致命错误。该文档在这里说明了这一点:construct">http://us.php.net/_construct

注意:尝试从析构函数(在脚本终止时调用)引发异常会导致致命错误。

大多数情况下你会得到这样的结果:

PHP Fatal error: Exception thrown without a stack frame in Unknown on line 0
Run Code Online (Sandbox Code Playgroud)

一个好的方法是不要重新发明轮子,而是采用 Doctrine ( http://www.doctrine-project.org/ ) 来帮助您完成这项任务。即使您无法使用这个软件,您也可以查看源代码以了解 Benjamin 是如何做到的。查看EntityManager(https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/EntityManager.php)和UnitOfWork(https://github.com/doctrine/doctrine2/blob/master ) /lib/Doctrine/ORM/UnitOfWork.php),这与您在问题中提到的处理有关。