Part II. Creational Design Patterns

Imagination is the beginning of creation. You imagine what you desire, you will what you imagine and at last you create what you will.

George Bernard Shaw

Without culture, and the relative freedom it implies, society, even when perfect, is but a jungle. This is why any authentic creation is a gift to the future.

Albert Camus

If the Lord Almighty had consulted me before embarking on creation thus, I should have recommended something simpler.

Alfonso the Wise

Creational design patterns are those that focus on the instantiation process. These patterns are designed to conceal the creation process from the instances created and encapsulate knowledge used by the objects. The five creational design patterns listed by Gamma, Helm, Johnson, and Vlissides include:

  • Abstract Factory

  • Builder

  • Factory Method

  • Prototype

  • Singleton

Of these five, the Factory Method and Prototype have been selected as examples of creational designs implemented in PHP. The Factory Method is the only one of the five that is included in the class scope category of design patterns, and it is a relatively simple yet informative pattern. The Prototype pattern is in the object category, and it can be implemented using the PHP __clone() method. Objects are instantiated (created) on the basis of prototype, and then further objects are cloned from the instantiated object. I think you will find this pattern easy to use and quite handy.

Of the uses of creational patterns, the most interesting is that ...

Get Learning 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.