Home  >  Article  >  Backend Development  >  php: sample code for destructor call

php: sample code for destructor call

黄舟
黄舟Original
2017-07-02 10:29:111012browse

phpDestructorCall

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

As can be seen from the above example, phpthrows an exception (regardless of whether there is a try catch outside), directlyWhen exiting, the destructor will be called.

The above is the detailed content of php: sample code for destructor call. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn