Home > Backend Development > PHP Tutorial > php: sample code for destructor call

php: sample code for destructor call

黄舟
Release: 2023-03-12 10:54:01
Original
1028 people have browsed it

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";
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template