Home >Backend Development >PHP Tutorial >php: sample code for destructor call
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!