falltime Posted July 1, 2008 Share Posted July 1, 2008 I've been coding in PHP for over 6 years now, and I've always just instantiated the class object and assigned it to a variable within the same PHP file. I never had any issues with this approach and up until very recently, I never considered there to be any issue. However, after stumbling upon the Singleton design pattern I've become a bit confused, simply because while I understand the "what" and "how" of it, I've failed to fully grasp the "why." I guess all I am trying to ask is what are the advantages of implementing the singleton design pattern over my current method: Assume this is session.php: class Session { var $username; //Username given on sign-up var $time; function Session(){ $this->time = time(); $this->username = "falltime" } }; $session = new Session; I then use a "require_once("../include/session.php");" in scripts where I need it, and it is automatically instantiated and assigned. Frankly I don't see how and where I would ever have a problem with the multiple class objects being created, since require_once would (of course) only be called once. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/ Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 Okay, sessions are a bad example. Databases are usually the prime targets of the pattern in PHP, for a simple reason. If you need to use a database in any classes, you simply retrieve the same instance instead of having to pass it around in constructors. class foo { protected $db; public function __construct() { $this->db = DB:getInstance(); } } class bar { protected $db; public function __construct() { $this->db = DB:getInstance(); } } They both have the same database instance to work with. And for someone programming in PHP for 6 years, I'm shocked that you're using such horribly deprecated syntax. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579380 Share on other sites More sharing options...
.josh Posted July 1, 2008 Share Posted July 1, 2008 They both have the same database instance to work with. And for someone programming in PHP for 6 years, I'm shocked that you're using such horribly deprecated syntax. Maybe it has to do with the fact that he's not using php5. That's my guess, anyways. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579381 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 They both have the same database instance to work with. And for someone programming in PHP for 6 years, I'm shocked that you're using such horribly deprecated syntax. Maybe it has to do with the fact that he's not using php5. That's my guess, anyways. I guessed that at first too, then I realized that no one really uses PHP 4 any more. Like, at all. Most web hosts have upgraded and if they haven't they will soon, and probably all of the "all-in-one servers" (WAMP, etc.) have php5. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579384 Share on other sites More sharing options...
.josh Posted July 1, 2008 Share Posted July 1, 2008 hmm well in my own experience, most hosts still use php4 by default, but have php5 installed, as well, and you can use php5 code by using a specific mime type dedicated to it, like *.php5 (or assign your own). I really don't think php4 is gonna disappear from standard hosting packages until sometime after a php6 final is released. You got to understand, in the business world, the prevailing motto for coding is "If it ain't broke, don't fix it." The guys in suits don't give 2 sh!ts what's going on under the hood. If it's still working on the outside, they see no reason to change what's on the inside. It's only when stuff starts actually breaking (like register globals being turned off) that anybody actually does anything. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579389 Share on other sites More sharing options...
448191 Posted July 1, 2008 Share Posted July 1, 2008 I guessed that at first too, then I realized that no one really uses PHP 4 any more. Like, at all. Most web hosts have upgraded and if they haven't they will soon, and probably all of the "all-in-one servers" (WAMP, etc.) have php5. Not true, unfortunately. Anyway. There really isn't that much difference between your method and a Singleton. The most prominent property they share is that they SUCK. Yes, SUCK capitalized. Ok, actually they both couple the class and any using it to the global space, but to say they SUCK, is just so much easier. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579392 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 Whoa, SOMEONE doesn't like static variables. o-O I'm going to have to disagree. I generally shy away from statics too, but in the case of Singletons, it's an established design pattern that gets the job done. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579397 Share on other sites More sharing options...
448191 Posted July 1, 2008 Share Posted July 1, 2008 I'm not saying I never use Singletons (though I try to minimize usage), I'm just saying they SUCK. It's like shopping. It SUCKS, but you need new clothes so there's no viable way around it.. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579404 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 Oh. I was saying to myself "Wow, what the hell, why would he not use Singletons...?". =P There are some things that you just can't escape in OOP I guess. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579408 Share on other sites More sharing options...
448191 Posted July 1, 2008 Share Posted July 1, 2008 They CAN be avoided altogether, but sometimes that means so much extra work and complexity, it just isn't worth it. But why NOT use Singletons? I already told you. If you don't know what that means, look it up. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579421 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 They CAN be avoided altogether, but sometimes that means so much extra work and complexity, it just isn't worth it. But why NOT use Singletons? I already told you. If you don't know what that means, look it up. No, I know why someone wouldn't want to use them, but sometimes it's just necessary unless you want to have to think of another way around it. I'm not stupid. ;P Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579428 Share on other sites More sharing options...
448191 Posted July 1, 2008 Share Posted July 1, 2008 Not knowing something doesn't make you stupid, just ignorant. Which isn't half as bad as it sounds. Anyway.. Going waaay off topic. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579435 Share on other sites More sharing options...
.josh Posted July 1, 2008 Share Posted July 1, 2008 But...G.I. JOE told me that knowing is half the battle... Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579443 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 But...G.I. JOE told me that knowing is half the battle... G.I Joe is also made of plastic. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579445 Share on other sites More sharing options...
.josh Posted July 1, 2008 Share Posted July 1, 2008 nono, he's a real American hero. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579446 Share on other sites More sharing options...
keeB Posted July 2, 2008 Share Posted July 2, 2008 I use Singletons for one real application: The Abstract Factory [1] pattern. I've never thought to instantiate an object in the actual .class file. I guess I am really against that because it really would confuse me. Readability is so huge to me. [1]: http://en.wikipedia.org/wiki/Abstract_Factory Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579752 Share on other sites More sharing options...
KevinM1 Posted July 2, 2008 Share Posted July 2, 2008 How do those of you who loathe singletons handle things typically associated with a registry? Any globals I can remove, the better. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-579937 Share on other sites More sharing options...
DarkWater Posted July 2, 2008 Share Posted July 2, 2008 @keeB: Yeah, I never instantiate an object inside the class file either. Then you end forget where you started it and where it's coming from and it all gets out of hand. @_@ Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-580033 Share on other sites More sharing options...
keeB Posted July 2, 2008 Share Posted July 2, 2008 How do those of you who loathe singletons handle things typically associated with a registry? Any globals I can remove, the better. I don't think anyone said they loathed them. I think people are just saying that sometimes they are a Necessary Evil -- like (potentially) your case. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-580234 Share on other sites More sharing options...
448191 Posted July 3, 2008 Share Posted July 3, 2008 A global Registry sucks too. I use to think Singleton Registry was lesser evil between it and multiple Singletons, but I've since come to realize that it doesn't really matter. Both suck. Now, when the opportunity is given, I prefer regular instance Registries local and relevant to the classes that use it. Much better cohesion and no global coupling. And no, I don't loathe Singletons, I just said they're like shopping. Quote Link to comment https://forums.phpfreaks.com/topic/112804-the-singleton-design-pattern/#findComment-580702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.