PHP deep clone object

后端 未结 2 1720
眼角桃花
眼角桃花 2020-12-16 12:48

The scenario: fetch an email template from the database, and loop through a list of recipients, personalising the email for each.

My email template is returned as a

相关标签:
2条回答
  • 2020-12-16 12:49

    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).

    0 讨论(0)
  • 2020-12-16 13:05

    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...
    
    0 讨论(0)
提交回复
热议问题