0

I have read a lot about singletons, most people agree that they are bad practice and to avoid them, any way possible. Most people say this because it's hard to debug apps that use them.

Now, creating a simple CMS, I tried a few approaches and they seem to me the best choice.

  1. Configuration data

That file is loaded on application start and I see no reason why I wouldn't use singleton pattern when calling config data throughout my application?

  1. Request data

Request data should store all info from php server variables (POST, GET, COOKIE) so it can be used to read and write (e.g.cookies) data using singleton throughout the application.

  1. Response buffer

I want to use response class (as singleton) that will hold all data that is rendered by my templates. So application can load all views render them one by one and store the echoed data in the response class, and at the end output entire document that is stored in response.

Questions for all examples: A) Is that really bad practice, and why? I see no evil here. B) Is there an alternative / better way?

Thanks!

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
kruno
  • 21
  • 5
  • possible duplicate of [Who needs singletons?](http://stackoverflow.com/questions/4595964/who-needs-singletons) – Mike B Feb 04 '13 at 17:30
  • 1
    I personally avoid them because they can make an application harder to test. Since this is very much an opinion though, I would say this question is not constructive. – shanethehat Feb 04 '13 at 17:34
  • 1
    Request data is already superglobal, so I don't see why you'd need to put it in a singleton. That said, singletons are bad because they blow apart encapsulation. Often, they're used simply because the coder doesn't understand how to share a resource between different layers of their app. That's exactly why Dependency Injection exists, and why in just about all cases, the answer to "Should I use a singleton" is actually "No, use a DI container" – Major Productions Feb 04 '13 at 17:36
  • @KevinM1 As for Request, I just wanted to have cleaner code for accessing and writing global variables :) I'm reading just now about DI concept, at first glance they seem same as singleton, with option to create more than single instance. But I'll read it throughout before making any statements. – kruno Feb 04 '13 at 18:22
  • @shanethehat I didn't said my opinion! And I gave constructive examples where I wish to use them and asked is there a reason I shouldn't use them for that examples. – kruno Feb 04 '13 at 18:26
  • @kruno, the difference between DI and singletons is that DI doesn't blow apart encapsulation. With singletons, since they're static, encapsulation is ignored, so something like `blah::getInstance()` is available regardless of context. It's global, which means it suffers from all the flaws associated with global variables. DI wires up dependencies with the classes that use them. DI containers usually sit at the top of the app as a factory, so there's no blending/destroying app layers. – Major Productions Feb 04 '13 at 18:52

1 Answers1

2

Is that really bad practice, and why, as I see no evil here?

Design patterns are suggestions, not standards. You can use them, hate them, call them "anti-pattern" and do whatever you want but that will just be your opinion. The gang of four and any blogger out there express their opinion about it just like the way you do, and guess what? it doesn't really matter.

Instead of asking yourself if someone consider it a bad practice, ask yourself: "Do I consider it a bad practice?". If the answer is no then go for it.

I'd suggest you to read both sides (pro-singleton and against it) and make your own opinion about it, before taking this decision. But in the end there's no right answer, it's just a matter what you decide.

Is there an alternative(better) way?

Generally speaking I tend to use Dependency Injection more than Singleton where I can. But if there's no way I could use DI and Singleton is an option for me then I would surely go for it. My suggestion is to learn the most important patterns and use whatever makes sense to you in that particular context.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • Thanks for answering. I'll look into DI. Although singleton looks like it was made for my third example where I can access, modify page output (response) throughout my entire application. – kruno Feb 04 '13 at 18:30