首頁  >  文章  >  後端開發  >  php:析構函數呼叫的範例程式碼

php:析構函數呼叫的範例程式碼

黄舟
黄舟原創
2017-07-02 10:29:11985瀏覽

php析構函數呼叫

class a{
    function construct(){
        echo "start to construct a obj\n";
    }   
    function pide($a,$b)
    {   
        //exit(1);//destruct called
        return $a/$b; //destruct called when $b=0;
    }   
    function add($a,$b)
    {   
        throw new Exception ("daone");//destruct called
        return $a+$b;
    }   
    function destruct(){
        echo "start to destruct\n";
    }   
}
try{
    $first = new a();
    //$r = $first->pide(4,0);
    $first->add(4,0);
}
catch(Exception $e) 
{
    echo "cath exception \n";
}

從上面的範例可以看出,php拋出異常(不管外面有沒有try catch),直接退出的情況下,都會呼叫析構函數

以上是php:析構函數呼叫的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn