Page 1 of 1

HTTP Request Class - Singleton?

Posted: Fri Aug 18, 2006 1:57 am
by Luke
I already asked this question about sessions and got a surprising and useful answer, so I'm wondering if there are any unforeseen issues about HTTP Request.

It makes sense to me to make my HTTP_Request object a singleton because it is basically a wrapper for some of php's superglobals. I can't see needing more than one instance of this EVER, and it needs no constructor arguments... which makes it even EASIER to make a singleton. Am I wrong again?

Posted: Fri Aug 18, 2006 2:03 am
by feyd
I avoid singletons more often than not. Why? They're difficult to (unit) test, and it's just a glorified global. If think you need a singleton, consider placing it in a Registry/Service Locator first.

Posted: Fri Aug 18, 2006 2:13 am
by Christopher
My question is what problem are you having that you are solving. Is you application creating many different Request objects and causing havoc? In most cases when you only want one instance of a class -- the solution is to only create one instance.

Posted: Fri Aug 18, 2006 2:18 am
by Luke
Imagepoint taken.

Posted: Fri Aug 18, 2006 3:58 am
by Oren
feyd wrote:I avoid singletons more often than not. Why? They're difficult to (unit) test, and it's just a glorified global. If think you need a singleton, consider placing it in a Registry/Service Locator first.
I already use a registry - the singleton registry. What's the point of having a registry that is not a singleton?, or in other words: what's the point of having 2 registered objects which are equal (note that I said equal, not identical)?

Posted: Fri Aug 18, 2006 8:56 am
by feyd
Oren wrote:I already use a registry - the singleton registry. What's the point of having a registry that is not a singleton?, or in other words: what's the point of having 2 registered objects which are equal (note that I said equal, not identical)?
There are situations where you might need multiple instances of the same class processing different things. It really depends on what you register and how your registry handles what you register.

Posted: Fri Aug 18, 2006 10:19 am
by Oren
Yeah, you are right. Ignore my previous post as I just woke up when I posted that one :P

Posted: Fri Aug 18, 2006 11:23 am
by Christopher
Oren wrote:I already use a registry - the singleton registry. What's the point of having a registry that is not a singleton?, or in other words: what's the point of having 2 registered objects which are equal (note that I said equal, not identical)?
Given your description, neither your Registry nor the objects it creates necessarily are Singletons. A Singleton is a class where every instance shares the same properties. That is not the same as a class that creates unique instances of classes.

Posted: Fri Aug 18, 2006 11:40 am
by Oren
First of all, read my previous post :wink:
Second, all I said was: I use the Singleton Registry
I never said that the objects I register are themselves singletons.