Home >Backend Development >PHP Problem >What is the php clone method?

What is the php clone method?

藏色散人
藏色散人Original
2020-10-14 16:40:313692browse

The php clone method refers to PHP object cloning. When we assign the integer $a to a variable $b, $b is a "copy" of $a, but the two are not related. $ A change in the value of b will not affect $a, and a change in the value of $a will not affect $b.

What is the php clone method?

Recommended: "PHP Video Tutorial"

PHP object cloning: __clone() method

Object cloning: __clone() method

When we assign the integer $a to a variable $b, $b is a "copy" of $a, but the second The two are irrelevant. Changes in the value of $b will not affect $a, and changes in the value of $a will not affect $b. The same is true for arrays, but it is different for objects. When the object instance $a is assigned to a variable $b, $b is not a "copy" of $a, but a reference to $a. Value changes will affect $a, and changes in the value of $a will also affect $b.

For example:

<?php
class Cat{
public  $name;
function __construct($name){
echo &#39;Cat类启动&#39;;
$this->name = $name;
}
function __destruct(){
echo &#39;Cat类结束&#39;;
}
}
$a = new Cat("默默");//实例化类,调用无参数的构造方法
//$c被销毁时自动调用析构方法
$b=$a;
echo "改变之前:<br>";
echo "a->name:".$a->name."<br>";
echo "b->name:".$b->name."<br>";
$a->name="琳琳";
echo "改变之后:<br>";
echo "a->name:".$a->name."<br>";
echo "b->name:".$b->name."<br>";
?>

Running results:

Before the Cat class starts to change :

a->name: Momo

b->name: Momo

After the change:

a->name: Linlin

b->name: Linlin

But many times we need a copy of an object, not just a reference to the object. At this time we can use the clone keyword, but be aware that if there is a reference in the "cloned" class attribute, the reference is retained. That is to say, the reference in the copy and the reference in the original class both point to the same memory. .

For example:

<?php
class Cat{
public $name;
function __construct($name){
echo &#39;Cat类启动&#39;;
$this->name = $name;
}
function __destruct(){
echo &#39;Cat类结束&#39;;
}
}
$a = new Cat("默默");//实例化类,调用无参数的构造方法
//$c被销毁时自动调用析构方法
$b=clone $a;
echo "改变之前:<br>";
echo "a->name:".$a->name."<br>";
echo "b->name:".$b->name."<br>";
$a->name="琳琳";
echo "改变之后:<br>";
echo "a->name:".$a->name."<br>";
echo "b->name:".$b->name."<br>";
?>

Run result:

Cat class starts changing before:

a->name: silently

b->name: Momo

After change:

a->name: Linlin

b->name: Momo

Cat Class end Cat class end

The attribute $name in this class is given a reference, so when you use the clone keyword to copy, the name in the "copy" and the $name in the "original" point to the same variable

The above is the detailed content of What is the php clone method?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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