Detailed explanation of the usage of php destructor

黄舟
Release: 2023-03-12 10:32:01
Original
2623 people have browsed it

void destruct (void)

PHP 5 introduces the concept of destructor, which is similar to otherobject-oriented languages , such as C++. A destructor is executed when all references to an object are removed or when the object is explicitly destroyed.

Destructor example

<?php
class  MyDestructableClass  {
   function  construct () {
       print  "In constructor\n" ;
        $this -> name  =  "MyDestructableClass" ;
   }

   function  destruct () {
       print  "Destroying "  .  $this -> name  .  "\n" ;
   }
}

$obj  = new  MyDestructableClass ();
?>
Copy after login

Like constructor, the destructor of the parent class will not be called secretly by the engine. To execute the parent class's destructor, parent::destruct() must be explicitly called in the child class's destructor body. In addition, just like the constructor, the subclass will inherit the parent class if it does not define a destructor.

The destructor will be called even when exit() is used to terminate the script execution. Calling exit() in the destructor will abort the rest of the shutdown operation.

Note:

The destructor is called when the script is closed, after all HTTP headers have been sent. It is possible that the working directory when the script is closed is different from when it is in a SAPI (such as apache).

Note:

Attempting to throw an exception in the destructor (which is called when the script terminates) will result in a fatal error.

class x
{
 function construct()
 {
  $this->file = fopen(&#39;path&#39;, &#39;a&#39;);
 }
 function destruct()
 {
  fclose($this->file);
 }
}
Copy after login

Simply put, the destructor is used to complete special work when the object is closed. For example, in the above example I wrote, a file is opened at the same time as it is instantiated, but when will it be closed? Just close it, so the destructor closes it directly,

Or during destruction, we write some of the processed data into the database. In this case, we can consider using the destructor to complete it. Before the destructor is completed, these object properties still exist and are only used for internal access, so you can safely do any aftermath work related to the object

The destructor is not intended to release the memory of the object itself. Instead, it is used to guide PHP where the memory that needs to be released is when the user needs to release some additional memory. Finally, PHP uses

when destructing. In general, we do not need to explicitly write fictitious functions. Unless You really have resources that need to be released.
To simply release, use the following.
unset(Variable name);
or $variable name = NULL;.

The above is the detailed content of Detailed explanation of the usage of php destructor. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!