PHP destructor Simple instructions for use, PHP instructions for use
With the universal deployment of object-oriented programming, object-oriented shows many interesting problems. It is believed that many beginners will be exposed to two of functions, constructors and destructors when they are object-oriented. Constructors seem to use more, destructors are less used (compared to beginners limited programming experience, the author is the same.) In the function, the constructor is called when the object is created, and the destructor is called when the object is destroyed, without having to call it deliberately, one end at a time, and a back and forth.
The transactions that the destructor often deals with are some of the resources released, such as fopen (), which calls Fclose (), preceded by IMAGECREATEFROMJEPG (), where Imagedestory () is called, and these are all common examples, of course not limited to this. We can treat it as a normal function that executes when the object is destroyed or the script is executed.
So much more, or as early as possible to raise the main question today:
<?phpclass test{Public Function __destruct () { echo "execution destructor";}} $test 1=new Test; $test 2= $test 3= $test 1;unset ($test 1); echo "
";
What is the result of this script execution?
Before answering this question, look back at the few words I have marked above. We can take it for granted that unset ($test 1) before the output divider will call the destructor and output the text, and as for $test 2, $test 3 should call the destructor after the script has finished executing. That is, a paragraph of text will be printed on top of the split line, and two paragraphs will be printed below the divider line. At this time, big can be small pride, after all, I know when to call the destructor. But is the reality really so? We can look at the results of the execution.
Hey, he meow, how to output a sentence ah???
In fact, we have overlooked an important precondition, that is, the assignment of the object is the default reference assignment. This is a lot of people do not notice, I hope beginners can pay more attention.
So since it is a reference assignment, combined with our understanding of common variables, we quickly think that three variable names point to the same storage address. So then, what is the role of unset ($test 1)??? Does the break variable point to the storage address or destroy the storage address store content?
For an understanding of the unset () function usage, intuitively skip this paragraph.
Thinking about the pain of the egg, like to look at the manual.
The same is the passing of a reference, destroying only the variable name pointing to the storage address. In conjunction with the usual unset () function, we can describe that when multiple variable names or object names point to a single storage address, the unset () function simply destroys the pointer to the variable name and the storage address, and when there is only one variable name or object name, Unset destroys the content on 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. Unset (), a person does not look, left, the TV is still open. When only one person watches TV, unset (), when the person leaves, to turn off the TV, that is, to release the occupied storage space. If you are interested in this part of the content, you can also take a look at the PHP recursive function of the three ways of implementation.
Okay, back to the subject. Unset ($test 1), the original object is still there. When the split line is output, the script finishes executing and calls the destructor. Because there is only one object, the call destructor is called only once. The output as a result is also rightfully.
Several other interesting questions: There are many more ways to call destructors in a program. Whether the object is set to null or FALSE, the remaining objects remain unaffected. There is a difference between this and the normal variable. (The unset () function has the same effect). If you are interested, you can try it.
Another embarrassing thing to say: we all know that constructors can use __construct (), but ignore constructors with the same name. So, let's pay attention.
Finally, let's look at an example.
<?php/* * Created on 2009-11-18 *-Change the template for this generated file go to * WINDOW-PR Eferences-phpeclipse-php-code Templates * /class student{ //attribute private $no; Private $name; Private $gender; Private $age; private static $count =0; function __construct ($pname) { $this->name = $pname; Self:: $count + +; } function __destruct () {self :: $count--; } static function get_count () { return self:: $count; } } $s 1=new student ("Tom"); Print (Student::get_count ()); $s 2=new student ("Jerry"); Print (Student::get_count ()); $s 1=null; Print (Student::get_count ()); $s 2=null;
The above code is PHP5 destructor of the specific use of the method, I hope to help you.
http://www.bkjia.com/PHPjc/1049132.html www.bkjia.com true http://www.bkjia.com/PHPjc/1049132.html techarticle simple instructions for using PHP destructors, PHP usage instructions with the universal deployment of object-oriented programming, object-oriented presents many interesting questions. I believe many beginners learn PHP for ...