With the widespread development of object-oriented programming, object-oriented programming has revealed many interesting problems. I believe that many beginners will come into contact with two functions when learning php object-oriented, constructor and destructor. Constructors seem to be used more, and destructors are used less (relative to the limited programming experience of beginners, the same is true for the author.) Functionally, the constructor is called when creating the object, and the destructor is called The function is called when the object is destroyed, and there is no need to call it specially.
The destructor often handles some resource release work. For example, fopen() is called before, fclose() is called here, imagecreatefromjepg() is called before, and imagedestory() is called here. These are some common Examples are, of course, not limited to these. We can treat it as an ordinary function that will be executed when theobject is destroyed or the script is executed.
It’s so long-winded, let’s raise today’s main question as soon as possible:<?php class Test{ public function destruct(){ echo "执行析构函数"; } } $test1=new Test; $test2=$test3=$test1; unset($test1); echo "<hr/>";
unset($test1), this will call the destructor and output the text. As for $test2,$test3 The destructor should be called after the script is executed. That is to say, one paragraph of text will be output above the dividing line, and two paragraphs of text will be output below the dividing line. At this time, you can be a little proud, after all, you know when to call the destructor. But is this really the case? We can take a look at the execution results.
Hey, damn, why did you just output one sentence? ? ? In fact, we have ignored an important prerequisite, that is, the assignment ofobjects is by reference assignment by default. Many people have not noticed this, and I hope beginners can pay more attention to it.
Since it is a reference assignment, combined with our understanding of ordinary variables, we quickly thought that the three variable names point to the same storage address. So if that's the case, what role doesunset($test1) play? ? ? Destroy the variable pointing to the storage address or destroy the content stored in the storage address?
If you understand the usage of the unset() function, please skip this paragraph intuitively. Thinking about this painful problem, why not check the manual. The same is passed by reference, only the variable name pointing to the storage address is destroyed. Combined with the normal function of unset(), we can describe it like this,When multiple variable names or object names point to a storage address, the function of the unset() function is only to destroy the pointers of the variable names and storage addresses. When there are only For a variable name or object name, unset destroys the content at the specified storage address.
We can imagine that the real storage content is a TV. Multiple people (multiple variable names or object names) are watching a TV. After unset(), no one stopped watching and left, but the TV was still on. When only one person is watching TV, after unset(), when the person leaves, the TV must be turned off, which is to release the occupied storage space. If you are interested in this part of the content, you can also take a look at "Three Implementation Methods of PHPRecursive Function".
Okay, back to the topic.unset($test1)After that, the original object is still there. When the dividing line is output, the script is executed and the destructor is called. Because there is only one object, calling the destructor is only called once. It is natural to output the above results.
Several other related interesting questions: There are many ways to call the destructor in the program. Regardless of whether the object is set to null or false, other objects remain unaffected. This is still different from ordinary variables. (The effect of the unset() function is the same). If you are interested, you can try it. Another embarrassing thing: we all know that the constructor can use construct(), but we ignore the constructor with the same name. So, everyone should pay attention.The above is the detailed content of Solving a problem with php destructor. For more information, please follow other related articles on the PHP Chinese website!