Jump to content

The Singleton Design Pattern


falltime

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.