PHP deep clone object

陌路散爱 提交于 2019-11-29 03:19:39

You could add a __clone() method to your email class. Which is automatically called when an instance of this class is cloned via clone(). In this method you can then manually add the template.

Example:

class email {
    __clone() {
         $this->template = new template();
    }
}

.

unserialize(serialize($object)); // would be another solution...

Another more generic and powerful solution: MyCLabs\DeepCopy.

It helps creating deep copy without having to overload __clone (which can be a lot of work if you have a lot of different objects).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!