php __clone()和“ shallow clone” [英] php __clone() and the "shallow clone"

查看:62
本文介绍了php __clone()和“ shallow clone”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当__clone()的结果是浅克隆时,这是什么意思?

What is meant when the results of __clone() is a "Shallow Clone"?

推荐答案

简而言之:克隆将与从其克隆的原始对象保持相同的引用。诸如字符串或整数之类的原始类型从不引用(在php中),并且如果完全更改一个引用(通过将属性的对象替换为另一个对象),这也不会影响原始对象。每个属性将包含相同,而不仅包含相同对象,还包含其他对象的同名属性。

In short: A clone will remain the same references as the original object it is cloned from. Primitive types like strings, or integer are never references (in php) and if you change one reference completely (by replacing the object of a property with another one), this will also not affect the original object. Every property will contain the same and not only the identical object, than the same-named property of the other object.

要创建非吞咽副本,必须实现 __ clone()

To create non-swallow copies you must implement __clone(). This is called on the cloned object after the cloning.

public function __clone () {
  $this->myObject = clone $this->myObject;
  // and so on
}

这篇关于php __clone()和“ shallow clone”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆