With the widespread development of object-oriented programming, object-oriented display of many interesting problems. It is believed that many beginners will be exposed to two functions, constructors and destructors when learning php object-oriented objects. Constructors seem to use a lot more, with less destructor (as opposed to beginner's limited programming experience). Functionally, the constructor is invoked when the object is created, and the destructor is invoked when the object is destroyed, without having to call it intentionally, one end, and then the other.
The transaction that a destructor often deals with is the work of releasing resources, such as fopen (), where the fclose () is called, and the Imagecreatefromjepg (), which is called imagedestory (), is a common example, which is certainly not limited to this. We can use it as a normal function that will be executed when the object is destroyed or when the script finishes executing.
With so much chatter, it is time to raise the main questions of today:
<?php
class 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 you answer this question, look back at the few words I have indicated above. We can take it for granted that unset ($test 1) before the output divider will invoke the destructor, output the text, and as for $test 2, $test 3 should call the destructor at the end of the script execution. That is, on top of the split line, a piece of text is output, and two paragraphs are output below the divider line. At this time, can be small proud, after all, they know when to call the destructor. But is the reality really so? We can look at the execution results.
Hey, he meow, how to output a sentence ah???
In fact, we have overlooked an important prerequisite, that is, the assignment of an object is by default a reference assignment. Many people do not notice this, I hope beginners can pay more attention to it.
So since it's a reference assignment, combined with our understanding of ordinary variables, we quickly realized that three variable names point to the same storage address. So what is the effect of unset ($test 1)??? Does the damage variable point to the storage address or to the contents of the storage address store?
To learn about the use of unset () function, skip this paragraph.
Thinking about the pain of the egg, no matter how to check the manual.
The same is passing the reference, destroying only the variable name point to the storage address. Combined with the usual unset () function, as we can describe, when multiple variable names or object names point to a storage address, the unset () function is simply to destroy the variable name and the location of the store address, when only one variable name or object name, Unset destroys the contents of the specified storage address.
We can imagine that the real storage content is a TV set. Multiple people (multiple variable names or object names) looking at a TV set. Unset (), a person does not look, left, the TV is still open. When only one person watching TV, unset (), when people leave, to turn off the TV, that is, to release the storage space occupied. If you are interested in this part of the content, you can also look at "PHP recursive function three ways to implement."
Okay, back to the subject. Unset ($test 1), the original object is still there. When the output is split, the script executes and the destructor is called. Because there is only one object, the call to the destructor is only once. The output is, as it were, a well-deserved result.
A few other pertinent interesting questions: There are many ways to call destructors in a program. Whether you set the object to null or FALSE, the remaining objects remain unaffected. There is a difference between this and the ordinary variable. (The effect of the unset () function is the same). You can try if you are interested.
Another embarrassing thing to say: we all know that constructors can use __construct (), but ignore constructors with the same name. So let's keep an eye on it.
Finally, let's look at an example
<?php/* Created on 2009-11-18 * * To change the template for this
generated file go to
* Window -Preferences-phpeclipse-php-code Templates
*
/class student{
//attributes
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;
Print (Student::get_count ());
? >
The above code is the PHP5 destructor of the specific use of the method, I hope to help.