Prototype pattern

The Prototype design pattern allows us to effectively duplicate objects while minimizing the performance impacts of having to re-instantiate an object.

You may have heard of prototypal languages if you've worked with JavaScript. In such languages, you work by cloning prototypal objects to create new objects; in turn, there is a reduced cost for creating new objects.

We have, so far, extensively discussed the use of the __construct magic method, but we haven't touched on the __clone magic method. The __clone magic method is what's run before an object is cloned (if possible); the method cannot be called directly and takes no parameters.

You might find it useful to use the __clone method when using this design pattern; that said, ...

Get Mastering PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.