PHP deep clone object

后端 未结 2 1725
眼角桃花
眼角桃花 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 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...
    

提交回复
热议问题