$this and $that pointers in PHP use instances, that pointer _php tutorial

Source: Internet
Author: User

$this and $that pointers in PHP use instances, that pointer


A special method named "__clone ()" method is defined in PHP5, which is the method that is called automatically when the object is cloned, and the "__clone ()" method will establish an object that has the same properties and methods as the original object, and if you want to change the contents of the original object after cloning, you need to __clone () Overriding the original properties and methods, the "__clone ()" method can have no parameters, it automatically contains the $this and $that two pointers, $this point to the replica, and the $that point to the original, the specific example is as follows:
Copy CodeThe code is as follows:
<?php
Class Person {
The following is the person's member property
var $name; Man's name
var $sex; Man's Sex
var $age; The age of the person
Define a constructor method parameter to assign a property 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 speak in a way that speaks his own attributes
function say () {
echo "My name is:". $this->name. "Gender:". $this->sex. "My Age is:". $this
->age. "
";
}
The method that is called automatically when the object is cloned, and if you want to change the contents of the original object after cloning, you need to override the original properties and Methods in __clone ().
function __clone () {
$this refers to the replica P2, while $that is pointing to the original P1, so that in this method, the properties of the replica are changed.
$this->name = "I am a copy of Zhang San $that->name";
$this->age = 30;
}
}
$p 1 = new Person ("Zhang San", "male", 20);
$p 2 = clone $p 1;
$p 1->say ();
$p 2->say ();
?>

The result of the successful operation of this PHP program is as follows:

Copy the Code code as follows:
My name is: Zhang San Sex: Male my age is: 20
My name is: I am copy of Zhang San Sex: Male my age is: 20

http://www.bkjia.com/PHPjc/938858.html www.bkjia.com true http://www.bkjia.com/PHPjc/938858.html techarticle The $this and $that pointers in PHP use an instance, that pointer PHP5 defines a special method named "__clone ()" method, which is the method that is called automatically when the object is cloned, using the "__clone ()" Method ...

  • Related Article

    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.