Php destructor simple instructions, php instructions _ PHP Tutorial

Source: Internet
Author: User
Simple use of php destructor, php instructions. Simple use of php destructor, php instructions with the widespread development of object-oriented programming, object-oriented shows many interesting problems. I believe that many beginners will learn simple instructions on using php destructor.

With the widespread development of object-oriented programming, object-oriented programming presents many interesting problems. I believe many beginners will come into contact with two functions, constructor and Destructor, when learning php object-oriented. Constructor seems to use more and less destructor (The same is true for beginners with limited programming experience .) In terms of functions, constructor is called when an object is created, and the destructor is called when the object is destroyed without the need to make a specific call.

The transactions frequently processed by destructor are resources released. for example, if fopen () is in the front, fclose () is called here, imagecreatefromjepg () is in front, and imagedestory () is called here (), these are common examples. We can regard it as a common function that will be executed when the object is destroyed or the script is executed.

If there are so many questions, we should raise the main questions as soon as possible:

<? Phpclass Test {public function _ destruct () {echo "execute destructor" ;}}$ test1 = new Test; $ test2 = $ test3 = $ test1; unset ($ test1); echo"
 ";

What is the execution result of this script?

Before answering this question, let's look back at the words marked above. We can reasonably consider it as unset ($ test1) before the output Separator. This will call the destructor and output the text. for $ test2, $ test3 should call the destructor after the script is executed. That is to say, a piece of text is output on the split line, and two sections of text are output under the split line. At this time, you can be proud of it. after all, you know when to call the destructor. But is it true? Let's take a look at the execution results.

Hey, why did he output a sentence ???

In fact, we ignore an important precondition, that is, the default value of the object is the reference value assignment. This is not noticed by many people. I hope beginners can pay more attention to it.

Since it is a reference value assignment, combined with our understanding of common variables, we soon thought that the three variable names point to the same storage address. So what is the role of unset ($ test1 ??? Does the damage variable point to the storage address or destroy the content stored in the storage address?

Skip this section if you understand the usage of the unset () function.

I thought about the problem. I don't want to go to the manual.

Similarly, when a reference is passed, only the variable name points to the storage address is destroyed. When multiple variable names or object names point to a storage address () the function only serves to destroy the point of the variable name and storage address. when there is only one variable name or object name, unset destroys the content on the specified storage address.

We can imagine that the actual storage content is a TV. Multiple people (multiple variable names or object names) watch a TV. After unset (), a person does not watch, leaves, and the TV is still on. When only one person watches TV, after unset (), when a person leaves, the TV will be shut down, that is, the occupied storage space will be released. If you are interested in this part of the content, you can also take a look at the three implementation methods of php recursive functions.

Okay. return to the topic. After unset ($ test1), the original object is still in. After the split line is output, the script is executed and the destructor is called. Because there is only one object, you can call the destructor only once. The output of the above results is justified.

There are several other interesting questions: there are still many methods to call the Destructor in the program. No matter whether the object is set to null or false, other objects are still not affected. This is different from common variables. (The effect of the unset () function is the same ). If you are interested, try it.

In addition, we all know that constructor can use _ construct (), but constructor with the same name is ignored. So you should pay attention to it.

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 {// 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 () {retu Rn self: $ count ;}}$ s1 = new student ("Tom"); print (student: get_count ()); $ s2 = new student ("jerry"); print (student: get_count (); $ s1 = NULL; print (student: get_count ()); $ s2 = NULL; print (student: get_count ();?>

The above code is the specific usage of the PHP5 destructor. I hope it will be helpful to you.

With the widespread development of object-oriented programming, object-oriented programming presents many interesting problems. I believe that many beginners learn php...

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.