Example of using $this and $that pointers in PHP
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 () overrides the original property and method, the "__clone ()" method can have no parameters, it automatically contains $this and $that two pointers, $this point to the replica, and $that point to the original, the specific example is as follows:
The code is as follows:
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:
The code is 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/991644.html www.bkjia.com true http://www.bkjia.com/PHPjc/991644.html techarticle php $this and $that pointers use instance PHP5 to define a special method name __clone () method, which is the method that is called automatically when the object is cloned, and the __clone () method will be used to set up an original pair ...