php中__clone() shallow copy 只是浅复制
什么是浅复制呢?
简单一点,就是说复制一个对象的时候,如果对象$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 只是浅复制的更多相关文章
- python中的shallow copy 与 deep copy
今天在写代码的时候遇到一个奇葩的问题,问题描述如下: 代码中声明了一个list,将list作为参数传入了function1()中,在function1()中对list进行了del()即删除了一个元素. ...
- iOS中的深复制与浅复制
很多语言中都有深复制浅复制的概念,如C++,ObjC等.简单来说,浅复制就是两个变量指向了同一块内存区域,深复制就是两个变量指向了不同的内存区域,但是两个内存区域里面的内容是一样的. 浅复制示意图: ...
- C#中的深复制与浅复制
C#中分为值类型和引用类型,值类型的变量直接包含其数据,而引用类型的变量则存储对象的引用. 对于值类型,每个变量都有自己的数据副本,对一个变量的操作不可能影响到另一个变量.如 class Progra ...
- clone中的浅复制和深复制
clone:用于两个对象有相同的内容时,进行复制操作. 提示:Java中要想自定义类的对象可以被复制,自定义类就必须实现Cloneable中的clone()方法. 浅复制:另一个对象用clone()方 ...
- Python赋值、浅复制和深复制
Python赋值.浅复制和深复制 首先我们需要知道赋值和浅复制的区别: 赋值和浅复制的区别 赋值,当一个对象赋值给另一个新的变量时,赋的其实是该对象在栈中的地址,该地址指向堆中的数据.即赋值后,两 ...
- java 及 Jquery中的深复制 浅复制
发现问题:最近 遇到由于复制对象之后,改变复制后的新变量,原先被复制的对象居然会跟着变. EX:java中: //holidayConfig.getEnd_time()会随着sTime的改变而改变 s ...
- python中深复制与浅复制
转载:http://blog.csdn.net/vicken520/article/details/8227524 java中也经常碰见这种问题.时间原因就不写java方面啦 Python深复制浅复制 ...
- retain copy(浅复制) mutablecopy (深复制)
http://blog.csdn.net/xdrt81y/article/details/24331103 口诀: 1浅3深 NSArray copy (浅) 返回NSArray NSArra ...
- iOS-深复制(mutableCopy)与浅复制(copy)
浅复制:只复制指向对象的指针,而不复制引用对象本身.对于浅复制来说,A和A_copy指向的是同一个内存资源,复制的只是一个指针,对象本身资源还是只有一份(对象引用计数+1),那如果我们对A_copy执 ...
随机推荐
- 微信支付WxpayAPI_php_v3(一)sdk简介与错误修改
经过断断续续将近一周的时间终于把微信支付调通了. 这里总结一下,算是给后来者有个指引.少踩坑!!!! 开发语言:php5.5 语言框架:laravel5.2 微信sdk:WxpayAPI_php_v3 ...
- 文件描述符和exec() close_on_exec
#include <fcntl.h> #include <iostream> #include <unistd.h> using namespace std; in ...
- OpenGL------在Windows系统中显示文字
增加了两个文件,showline.c, showtext.c.分别为第二个和第三个示例程序的main函数相关部分.在ctbuf.h和textarea.h最开头部分增加了一句#include <s ...
- mysql中exists的用法介绍
SELECT c.CustomerId, CompanyName 2 FROM Customers c 3 WHERE EXISTS( 4 SELECT OrderID FROM ...
- 分布式数据库Cobar
Cobar简介: Cobar是关系型数据库的分布式处理系统,它可以在分布式的环境下看上去像传统数据库一样为您提供海量数据服务. 产品在阿里巴巴B2B公司已经稳定运行了3年以上. 目前已经接管了3000 ...
- angular.js_$scope
Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Scope 可应用在视图和控制器上. Angular ...
- js date相关学习!
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...
- CentOS下编译安装Apache2(新)
官网下载apache,apr, apr-util,pcre httpd-2.4.16.tar.gz http://httpd.apache.org/download.cgi#apache24 apr- ...
- Roboguice学习之视图注入
Robuguide的使用 准备工作: 首先在项目中必须实现GreetingModule和RoboguiceDemoApplication GreetingModule.java import com. ...
- Unity中www的基本应用
Unity的www主要支持HTTP中的GET和POST方式,GET方式会将请求附加到URL后,POST方式则是通过FORM的形式提交. 以下为Unity客户端的信息: using UnityEngin ...