$ This and $ that pointer in PHP use the instance _ PHP Tutorial

Source: Internet
Author: User
In PHP, the $ this and $ that pointers use instances. In PHP, the $ this and $ that pointers define a special method named _ clone () using the instance PHP5, which is automatically called during object cloning, use the _ clone () method to create an instance with the $ this and $ that pointers in the original PHP.

PHP5 defines a special method named "_ clone ()", which is automatically called during object cloning and uses "_ clone () the method creates an object with the same attributes and methods as the original object. to change the content of the original object after cloning () override the original attributes and methods. the "_ clone ()" method can have no parameters. it automatically contains two pointers $ this and $ that, and $ this points to the duplicate, and $ that points to the original, the specific instance is as follows:

The code is as follows:

Class Person {

// The following are the member attributes of a person.

Var $ name; // The name of a person.

Var $ sex; // gender of a person

Var $ age; // age of a person

// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.

// Function _ construct ($ name = "", $ sex = "", $ age = "")

Function _ construct ($ name, $ sex, $ age ){

$ This-> name = $ name;

$ This-> sex = $ sex;

$ This-> age = $ age;

}

// This person can talk about his/her attributes.

Function say (){

Echo "My name is:". $ this-> name. "Gender:". $ this-> sex. "My age is:". $ this

-> Age ."

";

}

// Method automatically called during object cloning. if you want to change the content of the original object after cloning, you need to rewrite the original attributes and methods in _ clone.

Function _ clone (){

// $ This refers to the copy p2, and $ that points to the original p1, so that the attributes of the copy are changed in this method.

$ This-> name = "I'm copying Michael Jacob $ that-> name ";

// $ This-> age = 30;

}

}

$ P1 = new Person ("zhang san", "male", 20 );

$ P2 = clone $ p1;

$ P1-> say ();

$ P2-> say ();

?>

The result of successfully running this PHP program is as follows:

The code is as follows:

My name is Zhang San Gender: Male my age is: 20

My name is: I copied the three Gender: Male, my age is: 20

PHP5 defines a special method named _ clone (), which is automatically called during object cloning and uses _ clone () the method will establish a pair with the original...

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.