首页 > Web开发 > 详细

php析构函数疑惑

时间:2015-10-20 12:29:13      阅读:197      评论:0      收藏:0      [点我收藏+]
<?php
class Test {
    public $start;
    public $end;
    
    public function __construct() {
        echo "__construct<br />";
        $this->start = microtime(true);
    }
    
    public function test() {
        echo "in test()<br />";
        throw new Exception("error", 500); // 抛出异常 
    }
    
    public function __destruct() {
        echo "__destruct<br />";
    }
}

$test = new Test();
$test->test();

方法里抛出异常后,不会调用析构函数:

技术分享

try异常后能调用析构函数:

<?php
class Test {
    public $start;
    public $end;
    
    public function __construct() {
        echo "__construct<br />";
        $this->start = microtime(true);
    }
    
    public function test() {
        echo "in test()<br />";
        try { // try 异常
            throw new Exception("error", 500); // 抛出异常 
        } catch(Exception $e) {
        }
    }
    
    public function __destruct() {
        echo "__destruct<br />";
    }
}

$test = new Test();
$test->test();

技术分享

php析构函数疑惑

原文:http://my.oschina.net/banbo/blog/519231

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!