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!