什么是浅复制呢?

简单一点,就是说复制一个对象的时候,如果对象$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. QML中MouseArea元素的介绍

    原文:http://www.thisisqt.com/?action-viewnews-itemid-22 MouseArea元素的一个很典型的用法是和一个可视的item一起用,处理这个item的鼠标 ...

  2. Hibernate Tools for Eclipse安装

    声明:本文转载自 http://developer.51cto.com/art/200906/128067.htm Hibernate Tools for Eclipse Plugins 的安装和使用 ...

  3. memcached的安装(server、client)、magent整合

    声明:本编文章基于网络上的文章(90%),基本就是把我的安装步骤写一下,遇到问题记录一下 1.背景:项目需要多台服务器负载均衡,我们的应用有付费会员,不能让一个账号随便登陆,一个时间段只能一个账号,这 ...

  4. accept: Invalid argument linux 网络编程

    今天测试一个本地网络通讯,在ubuntu虚拟机下出现的问题,警报:accept: Invalid argument 初始化地方: socklen_t clilen;struct sockaddr_in ...

  5. Photoshop基础,前景背景,图层,选取

    1*前景色背景色 Alt+Delete 键 前景色填充 Ctrl+Delete 键 背景色填充 X 颜色转换 D 颜色互换 两个填充的原因: 2*图层(只要做东西就要建图层)透明的纸进行叠加,尽量多建 ...

  6. android全屏和取消全屏 旋转屏幕

    全屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 取消全屏 getWindow().clearFlags(Win ...

  7. windowSoftInputMode键盘把输入框挡住了

    android:windowSoftInputMode="stateHidden|adjustResize" >

  8. 利用npoi把多个DataTable导入Excel多个sheet中

    { 题外拓展:把datatable插入dataset DataTable fuben = new DataTable();//定义的datatablefuben = table.Tables[0].C ...

  9. 【多重背包模板】poj 1014

    #include <iostream> #include <stdio.h> #include <cstring> #define INF 100000000 us ...

  10. 使用jstl标签遍历双层的map(map下面的map)

    <c:forEach var="firstMap" items="${map}"> <c:forEach var="secondMa ...