Singletons Bad and Static Classes Bad
Posted: Tue Dec 08, 2009 1:21 am
I went looking on StackOverflow.com about singletons and also about static classes. Basically there's some debate.
They say singletons are bad because they basically make a global variable for you in a sense, and global vars are bad. But they are also bad because they can be hard to debug evidently.
As for static classes, they say they're bad because they are hard to test and hard to extend.
So, if you're building an MVC framework, and you leave out static classes and singletons, you are left with:
- your bootstrap is going to be pretty pathetic -- loading non-static classes, I guess, setting php.ini vars, and that's about it?
- when you need to use a class, you'll have to make an object var even if you're not holding any object data, and this was one of the reasons to use a static class, but, static classes are hard to test and hard to extend, they say.
- if you need a $Settings object for storing things like DSN and so on, you'll have to make a class with a function like get($sName), and then use a switch/case to return a value. Then, instantiate the object only when you need it. You'll also want to inject it into other classes where it is needed via public var.
- if you need a $DB object for holding your connection through a series of pages that are included, you'll need to instantiate it where you need it and then inject it into other classes where you need it.
I dunno -- hogwash. Seems like it's a trade-off here. I mean, static classes are there because they are for type of things that don't need object storage that multiple class methods can exchange data for. And singletons help you eliminate the need for global vars like that DB connection you might need, but sound like they should be used sparingly. Plus, a static class can be called inside a regular object class without injection or a global var. And then live with some of the unfortunate things about static classes and singletons.
What's your opinion?
They say singletons are bad because they basically make a global variable for you in a sense, and global vars are bad. But they are also bad because they can be hard to debug evidently.
As for static classes, they say they're bad because they are hard to test and hard to extend.
So, if you're building an MVC framework, and you leave out static classes and singletons, you are left with:
- your bootstrap is going to be pretty pathetic -- loading non-static classes, I guess, setting php.ini vars, and that's about it?
- when you need to use a class, you'll have to make an object var even if you're not holding any object data, and this was one of the reasons to use a static class, but, static classes are hard to test and hard to extend, they say.
- if you need a $Settings object for storing things like DSN and so on, you'll have to make a class with a function like get($sName), and then use a switch/case to return a value. Then, instantiate the object only when you need it. You'll also want to inject it into other classes where it is needed via public var.
- if you need a $DB object for holding your connection through a series of pages that are included, you'll need to instantiate it where you need it and then inject it into other classes where you need it.
I dunno -- hogwash. Seems like it's a trade-off here. I mean, static classes are there because they are for type of things that don't need object storage that multiple class methods can exchange data for. And singletons help you eliminate the need for global vars like that DB connection you might need, but sound like they should be used sparingly. Plus, a static class can be called inside a regular object class without injection or a global var. And then live with some of the unfortunate things about static classes and singletons.
What's your opinion?