Functions and usage of php object-oriented destructor

Source: Internet
Author: User
The previous article explains what constructor is. This section describes the corresponding destructor. The role of the destructor is the opposite of that of the constructor. it is called when the object is destroyed, and the role is to release the memory. The format of the destructor is __destruct (). like the constructor, the first two underscores are also _ & quot ;. The example gives us a deeper understanding of the usage of the parser. The previous article explains php object-oriented constructor. in this section, we will talk about the corresponding destructor.

The role of the destructor is the opposite of that of the constructor. it is called when the object is destroyed, and the role is to release the memory. The format of the destructor is __destruct (). like the constructor, the first two underscores (_) are used "_".

Functions and usage of destructor

The example gives us a deeper understanding of the usage of the parser.

Class Preson {public $ name; // defines the variable public $ age; public $ sex; public $ height; function _ construct ($ name, $ age, $ sex, $ height) {$ this-> name = $ name; // assign a value to the variable $ this-> age = $ age; $ this-> sex = $ sex; $ this-> height = $ height;} function _ destruct () {echo "the object has been destroyed"; }}$ Preson1 = new Preson (" ", "20", "female", "180"); echo $ Preson1-> name;

The running result is:

After running, the object is destroyed.

Precautions for using destructor:

Php uses a "garbage collection" mechanism to automatically clear objects that are no longer in use and release the memory. that is to say, the destructor will be automatically called even if the unset function is not used, here, we will only clarify when the destructor will be called. Generally, you do not need to manually wear the component analysis method.

The above is a detailed description of the role and usage of php object-oriented destructor. For more information, see other related articles in the first PHP community!

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.