__destruct ()
This method is automatically called internally by the system when the object is destroyed.
Divided into three categories:
1. This method is automatically called when the PHP code declaration period finishes;
2. When the object is unset (), the system automatically calls the method
3. When the object is re-assigned, the system automatically calls the method
1 classtest{2 3 Public function__destruct () {4 5 Echo"The code executed after the object is destroyed";6 }7 }8 9 $test=NewTest ();Ten One unset($test); A - $test= ' AAAA '; - the what is the function of the destructor: - is a snippet of code that executes when a resource object is created to close a connection after it has finished running - The connection needs to be closed after the MySQL data connection is completed - fopen() Close when the open file handle finishes executing + destroy session after session end, destroy () - + A at the function of the destructor: - ensure data persistence updates. - Each object corresponds to each record of a table in the database. - - CREATE TABLE Student ( -Sid int Unsigned PrimaryKeyAuto_increment, inName varchar (+) notNULL, -Gender nuem (' Male ', ' female ') notNULL to ) + - the classStudent { * Public $sid; $ Public $name;Panax Notoginseng Public $gender; - the Public function__construct ($sid){//when creating an object + $sql= "Select Sid,name,gender from student where Sid =$sid"; A } the + Public functionSave () {//after the data is updated, it needs to be saved after each update. - $sql= "Update student set Name=$this->name, gender=$this->gender WHERE sid =$sid"; $ } $ - Public function__destruct () {//actions that need to be synchronized to the database after each update of student information - $this-Save (); the } - }Wuyi the $s=NewStudent (); - $s->name = ' Xujin '; Wu $s->save ();//This function is called every time because updating the data requires a synchronous update of the database. If you put this function in a destructor, the synchronous update that saves the implementation data of the update operation and the persistence of the data are called automatically each time the PHP code snippet finishes executing. - About--------------------------------------------------------------------------------------------------------------- ---------------------------- $ classtest{ - - Public $db _url= ' localhost:3306 '; - Public $db _user= ' Root '; A Public $db _pwd= ' Root '; + the Public function__construct () { - $link=mysql_connect($db _url,$db _user,$db _pwd);//Connect Database Operations $ } the the Public function__destruct () { the Mysql_close($link);//close this connection resource instead of destroying the object when the object is fired, the system automatically calls the destructor to execute the connection to close the resource. the } - } in the $dao=NewTest (); the unset($dao); TipsThe object is actually destroyed when the object is unset, but the MySQL connection resource still exists and can execute the SQL statement About the $sql= "Show Databases"; the $result=mysql_query($sql);//The result is a return value indicating that the resource connection persists after the object is destroyed the + ---------------------------------------------------------------------------------------------------------------- --------------------------- theTips: The destructor is not destroying the object itself, but is used to close the resource connection.
PHP's destructor study notes