Explanation of PHP destructor usage

Source: Internet
Author: User
void Destruct (void)

PHP 5 introduces the concept of destructors, similar to other object-oriented languages such as C + +. Destructors are removed when all references to an object are deleted or executed when an object is explicitly destroyed.

destructor Example

<?phpclass  mydestructableclass  {   function  construct () {       print  "in constructor\n";        $this, name  =  "Mydestructableclass";   }   function  Destruct () {       print  "destroying"  .  $this, name  .  " \ n ";   }} $obj  = new  mydestructableclass ();? >

As with constructors, the destructor of the parent class is not called by the engine. To execute a destructor for a parent class, you must explicitly call the parent::d estruct () in the destructor body of the subclass. Also, as with constructors, subclasses inherit the parent class if they do not have a destructor defined by themselves.

Destructors are called even when the script is run with exit (). Calling exit () in the destructor will abort the operation of the rest of the shutdown operation.

Note:

The destructor is called when the script is closed, and all HTTP header information is sent. The working directory when the script is closed is likely to be different from the SAPI (such as Apache).

Note:

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

Class x{function construct () {  $this->file = fopen (' path ', ' a '),} function destruct () {  fclose ($this->fil e); }}

In a nutshell, the destructor is a special work to do when the object is closed, such as the example I wrote, which opens a file at the same time as the instantiation, but when it is closed, it is closed, so the destructor closes it directly,

Or at the time of the destruction, we will handle some of the data are written into the database, then we can consider the use of destructors to complete, before the completion of the destruction of these object properties are still present, and only for internal access, so you can rest assured that the object related to any aftermath work

Destructors are not intended to release the memory of the object itself, but to use it to instruct PHP where the memory needs to be freed when the user needs to release some additional memory, and the last time PHP is used in the destructor

In general, we do not need to explicitly write imaginary functions. Unless you really have the resources you need to release.
Simple release using the following is possible.
unset (variable name);
or $ variable name = NULL;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.