Home > Backend Development > PHP Tutorial > Find out the difference between the two $this in PHP's __clone() method?

Find out the difference between the two $this in PHP's __clone() method?

WBOY
Release: 2023-03-01 20:44:01
Original
1122 people have browsed it

How to explain the two $this in the __clone() method of PHP? What do they mean?

<code>class Person
{
    // 下面是人的成员属性var $name; // 人的名字
    var $sex; // 人的性别
    var $age; // 人的年龄
              // 定义一个构造方法参数为属性姓名$name、性别$sex 和年龄$age 进行赋值
              // function __construct($name="", $sex="",$age="")
    function __construct($name, $sex, $age) {
        $this->name = $name;
        $this->sex = $sex;
        $this->age = $age;
    }
    // 这个人可以说话的方法, 说出自己的属性
    function say() {
        echo "我的名字叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this
        ->age . "<br>";
    }
    // 对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原来的属性和方法。
    function __clone() {
        // $this 指的复本p2, 而$that 是指向原本p1,这样就在本方法里,改变了复本的属性。
        $this->name = "我是复制的张三$this->name";
        // $this->age = 30;
    }
}

$p1 = new Person ( "张三", "男", 20 );
$p2 = clone $p1;
$p1->say ();
$p2->say ();</code>
Copy after login
Copy after login

Reply content:

How to explain the two $this in the __clone() method of PHP? What do they mean?

<code>class Person
{
    // 下面是人的成员属性var $name; // 人的名字
    var $sex; // 人的性别
    var $age; // 人的年龄
              // 定义一个构造方法参数为属性姓名$name、性别$sex 和年龄$age 进行赋值
              // function __construct($name="", $sex="",$age="")
    function __construct($name, $sex, $age) {
        $this->name = $name;
        $this->sex = $sex;
        $this->age = $age;
    }
    // 这个人可以说话的方法, 说出自己的属性
    function say() {
        echo "我的名字叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this
        ->age . "<br>";
    }
    // 对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原来的属性和方法。
    function __clone() {
        // $this 指的复本p2, 而$that 是指向原本p1,这样就在本方法里,改变了复本的属性。
        $this->name = "我是复制的张三$this->name";
        // $this->age = 30;
    }
}

$p1 = new Person ( "张三", "男", 20 );
$p2 = clone $p1;
$p1->say ();
$p2->say ();</code>
Copy after login
Copy after login

There are no two

$this in __clone(), only one $this. This $this points to the cloned new object, because the __clone() method is called in the new object called.
When performing cloning, PHP will first perform a shallow copy, create a new object, and copy all the attributes of the original object to the new object. For reference variables such as objects and resources, only their pointers are copied, but they are not cloned. If you need to make a deep copy of these properties, you need to clone them separately in __clone().
For example:

<code>class MyCloneable
{
    public $obj;
    
    function __clone()
    {
        $this->obj = clone $this->obj;
    }
}</code>
Copy after login

Note here that the two $this->obj both refer to the $obj attribute of the new class, because when cloning, the $obj of the new object has been shallowly copied from the original object. This is just because We want to make a deep copy, so we clone $this->obj again.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template