Creational design pattern in PHP


Am reading Matt Zandstra’s

PHP Objects, Patterns,and Practice

and right now, am in part 2, chapter 9 which talk about generating objects and he talked about

Singleton Pattern, Factory Method Pattern, Abstract Factory Pattern and Prototype design pattern.

To my knowledge,

Singleton pattern

is discourage,

Factory Method Pattern and Abstract Factory Pattern

to me will complicate things when your project start growing while the

Prototype pattern

uses the magic function


to create concrete products which according to what I learnt using the magic function is to be avoided as soon as possible except the

__construct() function

and the


Now, my question, which way is the best way to create object in PHP(of the design pattern)



I have not read your reference. Or if I did then I don’t remember doing so. But as usual with these sorts of questions, the only answer is: it depends.

To start with, the php new operator works amazingly well under many cases.

The singleton is indeed discouraged in many cases simply because it is global in nature. But also keep in mind that the singleton is specifically for when you want one and only one instance of a given object. So it really does not apply at all for most cases.

I really have not heard of using the prototype pattern for object creation in php. Seems more like a javascript sort of thing. So no opinions there. Cloning does have it’s purposes but I would not consider it to be widely used.

Not sure what the difference is between the Factory Method Pattern and Abstract Factory Pattern. I usually just think in terms of just using a factory. By and large, factories are a good thing. They offer considerable flexibility and can be injected and what not. I have not known them to complicate things.

There is also something called the Builder Pattern which is quite common. It works well for complex objects. Might have it’s own chapter in your book.

To wrap this up your question as written is too broad for a specific “always do this” answer. You need to look at each type of object to determine the best method to create it.

1 Like

closed #3

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.