$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 ...