什么是浅复制呢?

简单一点,就是说复制一个对象的时候,如果对象$Obj的一个属性的类型是引用类型的,比如 $person这个属性,指向的是一个 叫做 $objPerson的一个引用, 那么复制$Obj的时候,

新复制出来的 $Obj_copy这个对象 和原来的 $Obj 这个对象的 $person属性,都指向的是同一个对象 $objPerson. 并没有重新开辟内存,生成新的 $objPerson对象。。

这种复制比较简单, 所以就叫做 浅复制 shallow copy..

php __clone() and the “shallow clone”

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.

This means that when the object is cloned, any properties that are reference variables (variables that refer to other objects, rather than a value) will remain references.

A "non-shallow" clone would set the new object's to the values of those properties, rather than leaving them as references.

The clone keyword in PHP represents a shallow copy.

In order to achieve a deep copy, you need to implement the magic method __clone

If you clone an object that has a member which is an object of another class with the simple clonekeyword, you would be keeping the same reference to that second object.

That's where the deep copy comes in handy, with something like this:

public function __clone()
{
$this->someOtherObject = clone $this->someOtherObject;
}

With that, you guarantee the clone will be deep, meaning it will clone the member objects as well, instead of just keeping the original reference to them.

------------------------------------------------------分割线----------------------------------------------------------

补充: 私有化的 __clone()  , 对于单例的对象是禁止 deep copy 的, 单例对象只允许 有一个对象, 一般不允许存在多个实例。否则就违反了单例这种设计模式的初衷。

/**
* Private clone method to prevent cloning of the instance of the
* *Singleton* instance.
*
* @return void
*/
private function __clone()
{
}

  

php中__clone() shallow copy 只是浅复制的更多相关文章

  1. python中的shallow copy 与 deep copy

    今天在写代码的时候遇到一个奇葩的问题,问题描述如下: 代码中声明了一个list,将list作为参数传入了function1()中,在function1()中对list进行了del()即删除了一个元素. ...

  2. iOS中的深复制与浅复制

    很多语言中都有深复制浅复制的概念,如C++,ObjC等.简单来说,浅复制就是两个变量指向了同一块内存区域,深复制就是两个变量指向了不同的内存区域,但是两个内存区域里面的内容是一样的. 浅复制示意图: ...

  3. C#中的深复制与浅复制

    C#中分为值类型和引用类型,值类型的变量直接包含其数据,而引用类型的变量则存储对象的引用. 对于值类型,每个变量都有自己的数据副本,对一个变量的操作不可能影响到另一个变量.如 class Progra ...

  4. clone中的浅复制和深复制

    clone:用于两个对象有相同的内容时,进行复制操作. 提示:Java中要想自定义类的对象可以被复制,自定义类就必须实现Cloneable中的clone()方法. 浅复制:另一个对象用clone()方 ...

  5. Python赋值、浅复制和深复制

    Python赋值.浅复制和深复制 ​ 首先我们需要知道赋值和浅复制的区别: 赋值和浅复制的区别 赋值,当一个对象赋值给另一个新的变量时,赋的其实是该对象在栈中的地址,该地址指向堆中的数据.即赋值后,两 ...

  6. java 及 Jquery中的深复制 浅复制

    发现问题:最近 遇到由于复制对象之后,改变复制后的新变量,原先被复制的对象居然会跟着变. EX:java中: //holidayConfig.getEnd_time()会随着sTime的改变而改变 s ...

  7. python中深复制与浅复制

    转载:http://blog.csdn.net/vicken520/article/details/8227524 java中也经常碰见这种问题.时间原因就不写java方面啦 Python深复制浅复制 ...

  8. retain copy(浅复制) mutablecopy (深复制)

    http://blog.csdn.net/xdrt81y/article/details/24331103   口诀: 1浅3深   NSArray copy (浅) 返回NSArray NSArra ...

  9. iOS-深复制(mutableCopy)与浅复制(copy)

    浅复制:只复制指向对象的指针,而不复制引用对象本身.对于浅复制来说,A和A_copy指向的是同一个内存资源,复制的只是一个指针,对象本身资源还是只有一份(对象引用计数+1),那如果我们对A_copy执 ...

随机推荐

  1. Nginx运行Laravel的配置

    修改nginx.conf.修改前记得备份一下,万一改错了还能还原回去. server { listen 80; server_name localhost; set $root_path '/usr/ ...

  2. Sockets

    Sockets time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  3. JSTL标签库---SUN公司开发的标签库

    JSTL里的标签包含五大类标签: 核心标签库 国际化标签 JSTL函数(EL函数) 数据库标签// 操作数据库的,用不到了 XML标签//操作XML的,用不到了 都在jstl.jar,standar. ...

  4. linux视频学习6(mysql的安装/)

    1.mysql的优点: 免费,跨平台,轻,支持多并发. 2.mysql的安装步骤: 把安装文件准备好,拷贝到home目录下.mount /mnt/cdrom cp mysql* /home 把安装文件 ...

  5. jquery的校验规则的方法

    //validate 选项*********************************************************** $("form").validat ...

  6. Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整(原理:底层SurfaceView+上层绘制ImageView)

    Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整实现(原理:底层SurfaceView+上层绘制ImageView) 分类: Android开发 Androi ...

  7. 简易控制中心,angular的简单使用

    <html> <head> <meta charset='utf-8'> <script src="js/angular.js">& ...

  8. C#,js数据排序及其操作

    List<int> listint=new List<int>{2,1,7,3,8,5,4,6};listint.Sort((x, y) => x - y); var a ...

  9. java 创建一个File文件对象

    Example10_1.java import java.io.*; public class Example10_1 { public static void main(String args[]) ...

  10. Problem H: 小火山的围棋梦想 多校训练2(小火山专场)

    题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1908 题意:如果'.'被'*'围起来,就把'.'变为'*'. 分析:如果是'*'直接输出, ...