HTTP Request Class - Singleton?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

HTTP Request Class - Singleton?

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Imagepoint taken.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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)?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Yeah, you are right. Ignore my previous post as I just woke up when I posted that one :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
Post Reply