Simply put, the destructor is a special task used to complete when the object is closed. For example, the above example I wrote, open a file while instantiating, but when will it be closed? It will be closed after use, so destructor The function directly closes it, or when destructuring, we write some processed data into the database. At this time, we can consider using the destructor to complete it. Before destructuring, these object properties still exist, and It is only used for internal access, so you can safely do any aftermath work related to the object. It is not to release the object's own memory, but to use it to guide the php to release it when the user needs to release some memory. Where does the memory exist? Finally, php is used when destructuring
The code copy is as follows:
class x
{
function __construct()
{
$this->file = fopen('path', 'a');
}
function __destruct()
{
fclose($this->file);
}
}