In the spirit of yesterday’s post about learning to do things better, here’s an example.

A few years ago I wrote a post about how to create a PHP Singleton Database Connection class. It gets a few hits every week.

In it I said, “There’s a debate raging among those who claim to know more than anyone else about whether singletons are really a good idea. My take is that sometimes they are and sometimes they aren’t.”

Well, now I’m that someone who knows more now than I knew then. I think my claim against myself is legit. I should know. I was that guy once.

I never use it now. Ever. And I don’t recommend anyone else use it either.

Yes, singletons are a cool concept – a self-limiting class always returning the same instance – yeah, cool.

BUT singletons are rarely cool to actually use.

(I saw an article where Bob Martin used one once, but he’s Bob Martin and you and I are not Bob Martin — unless you happen to be Bob Martin, in which case, Hi Bob!)

So don’t use a singleton. Write some unit tests and then write a connection class. Then instantiate it once.

Please and thank you!