PHP 中的对象复制与克隆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6972860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:43:30  来源:igfitidea点击:

Object copy versus clone in PHP

phpobjectclone

提问by ddamasceno

Consider the following:

考虑以下:

$object1 = new stdClass();
$object2 = $object1;
$object3 = clone $object1;

$object1->content = 'Ciao';

var_dump($object1);
 // Outputs object(stdClass)#1 (1) { ["content"]=> string(4) "Ciao" }
var_dump($object2);
 // Outputs object(stdClass)#1 (1) { ["content"]=> string(4) "Ciao" }
var_dump($object3);
 // Outputs object(stdClass)#2 (0) { }

Is it a normal PHP behavior that $object2has a content identical to $object1?

它是$object2内容与 相同的正常 PHP 行为$object1吗?

To me it sound like $object2is a reference to $object1instead of a copy. Cloning the object before changing the content does act like a copy. This behavior is different than what happens with variables and seems unintuitive to me.

对我来说,这听起来像是$object2引用$object1而不是副本。在更改内容之前克隆对象确实像一个副本。这种行为与变量发生的行为不同,对我来说似乎不直观。

回答by deceze

Yes, that's normal. Objects are always"assigned" by reference in PHP5. To actually make a copy of an object, you need to cloneit.

是的,这很正常。在 PHP5 中,对象总是通过引用“分配”。要实际制作对象的副本,您需要这样做clone

To be more correct though, let me quote the manual:

不过,为了更正确,让我引用手册

As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

从 PHP5 开始,对象变量不再包含对象本身作为值。它只包含一个对象标识符,它允许对象访问者找到实际的对象。当一个对象通过参数发送、返回或分配给另一个变量时,不同的变量不是别名:它们持有指向同一个对象的标识符的副本。

回答by hakre

That's normal and I won't consider this unintuitive (for object instances):

这是正常的,我不会认为这不直观(对于对象实例):

$object1 = new stdClass();

Assigns a new object instance to $object1.

将一个新的对象实例分配给$object1

$object2 = $object1;

Assigns the object instance to $object2.

将对象实例分配给$object2

$object3 = clone $object1;

Assigns an new object instance cloned from an existing object instance to $object3.

将从现有对象实例克隆的新对象实例分配给$object3

If it would not be that way, each time you need to pass a concrete object instance, you would need to pass it by reference. That's burdensome at least but PHP did so in version 4 (compare zend.ze1_compatibility_modecore ). That was not useful.

如果不是那样,每次需要传递一个具体的对象实例时,都需要通过引用传递它。至少这很麻烦,但 PHP 在第 4 版中做到了(比较zend.ze1_compatibility_modecore)。那没有用。

Cloning allows the object to specify how it get's copied.

克隆允许对象指定如何复制它

回答by PankajR

object copy vs object clone

对象复制与对象克隆

class test{
public $name;
public $addr;
}
// i create a object $ob
$ob=new test();

// object copy 

$ob2=$ob;

// in object copy both object will represent same memory address 
// example 
$ob->name='pankaj raghuwanshi';

// i am printing second object
echo $ob2->name;
// output is : pankaj raghuwanshi

// another example 

$ob2->name='raghuwanshi pankaj';
echo $ob->name;
// output is :  raghuwanshi pankaj

// it means in copy of object original and copy object share same memory place

now clone of an object

现在克隆一个对象

$ob1=clone $ob;

echo $ob1->name;  // output is :  raghuwanshi pankaj
echo $ob->name;   // output is :  raghuwanshi pankaj

 $ob1->name='PHP Clone';
 $ob->name='PHP Obj';

echo $ob1->name;  // output is :  PHP Clone
echo $ob->name;   // output is :  PHP Obj

// on the base of these output we can say both object have their own memory space 
// both are independent 

回答by user187291

Objects in php5 are essentially pointers, that is, an object variable contains only an address of the object data located somewhere else. An assignment $obj1 = $obj2only copies this address and doesn't touch the data itself. This may indeed appear counterintuitive, but in fact it's quite practical, because you only rarely need to have two copies of the object. I wish php arrays used the same semantics.

php5中的对象本质上是指针,即一个对象变量只包含位于别处的对象数据的地址。赋值$obj1 = $obj2只复制这个地址,不涉及数据本身。这可能确实看起来违反直觉,但实际上它非常实用,因为您很少需要拥有对象的两个副本。我希望 php 数组使用相同的语义。