您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

对象的深拷贝有没有什么高效的做法?

2025/11/26 2:50:20发布19次查看
在clone对象的时候使用深拷贝有没有什么通用且高效的方法?
现在我的做法是用反射类去遍历,但只是一层的,这样做的话是不是对象内的对象依旧是引用的?
那么有没有人能提供一下多层深拷贝的做法呢?
public function __clone(){ $reflectclass = new \reflectionclass(__class__); foreach ($reflectclass->getproperties() as $properity) { $name = $properity-> name; if (\is_object($this->$name)) $this->$name = clone $this->$name; }}

回复内容: 在clone对象的时候使用深拷贝有没有什么通用且高效的方法?
现在我的做法是用反射类去遍历,但只是一层的,这样做的话是不是对象内的对象依旧是引用的?
那么有没有人能提供一下多层深拷贝的做法呢?
public function __clone(){ $reflectclass = new \reflectionclass(__class__); foreach ($reflectclass->getproperties() as $properity) { $name = $properity-> name; if (\is_object($this->$name)) $this->$name = clone $this->$name; }}

不需要用到的反射的呀,单个对象只需要关心自己的属性里又哪些是需要深拷贝的,直接调用 clone 拷贝即可,至于那个对象又是如何处理他的深拷贝的,应该交给他自己去处理。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product