Summary of the specific use of PHP destructors

  • 2020-03-30 02:20:54
  • OfStack

In simple terms, destructors are used to do special work when the object is closed. For example, I wrote the above example, open a file at the same time of instantiation, but when it is closed, close it after use, so destructors close it directly, Or when the destructor, we will deal with good together to write some data into the database, then consider using the destructor, before the completion of the destructor, these object properties are still exists, and is used only for internal access, so you can rest assured to do with the object of any the aftermath of the destructor is not in order to release the memory object itself, but when the user needs to release some extra memory when use it to guide the PHP need release where exists inside, finally using PHP in the destructor


class x 
{ 
function __construct()
{ 
$this->file = fopen('path', 'a'); 
}
function __destruct() 
{ 
fclose($this->file); 
}
}


Related articles: