addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1linklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

PHP as a Social Activity Message Board Project Support › Singletons, Good, Bad or Ugly?

Singletons, Good, Bad or Ugly?

Corry
user 17190121
Riverview, FL
Post #: 11
I was working a tutorial from netbeans and I got to a point where they had me change my procedural code to a class, but they used a singleton: http://netbeans.org/kb/docs/php/wish-list-lesson4.html­
Not really knowing what a singleton was before, I just barely remembered that I'd seen some saying not to use them.
http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx­

What is your opinion of them? I am so new at using classes that I don't have a clue as to what problems they might cause or solve.
Phillip
philsown
Raleigh, NC
Post #: 37
Basically a singleton is used for anytime you only want one instance of an object to exist in memory. That is, instead of this:

(sorry - the indentation gets clobbered)

class MyObj {
public $foo;
}

$a = new MyObj;
$a->foo = "bar";

$b = new MyObj;
$b->foo = "baz";

// this will be "bar"
echo $a->foo;

=======================

You have something like this:

class MyObj {
public $foo;
private $instance;

// make the constructor private so you can't say new MyObj() anymore
private function __construct() {}

public static function getInstance() {
if (null === self::$instance) {
// this calls the constructor here, because it's able to access
// the private constructor method from within the class

self::$instance = new self;
}
return self::$instance;
}
}

$a = MyObj::getInstance();
$a->foo = "bar";

$b = MyObj::getInstance();
$b->foo = "baz";

// this will be "baz", since $a and $b are the same instance of MyObj
echo $a->foo;

=======================

This is handy for stuff like site-wide config, or database objects (so the instance has a single connection instead of opening connections left and right).

The argument against is: It basically reintroduces GLOBAL variables - which are bad.

Another interesting thing you can do with it is a singleton of singletons... Maybe a DB class that has 2 different connections, one for read and one for write - like in the case of a replicated environment.
Sam L.
samuelelliot
Group Organizer
Saint Petersburg, FL
Post #: 46
Phillip is dead on, the singleton is a object oriented pattern that limits the access to its constructor and manages one instance of itself at a time.


OODesigns.com offers this description of When to Use the Singleton Pattern:
Singleton pattern should be used when we must ensure that only one instance of a class is created and when the instance must be available through all the code. A special care should be taken in multithreading environments when multiple threads must access the same resources thought the same singleton object.

OODesigns.com expresses Common Usage of the Singleton Pattern:
There are many common situations when singleton pattern is used:

  • Logger Classes
  • Configuration Classes
  • Accessing resources in shared mode
  • Other design patterns implemented as Singletons: Factories and Abstract Factories, Builder, Prototype

So your question of our opinion of Singletons in general, their pros and cons to an effect, can be answered with vastly differing opinions from cynics and optimists alike. However, when it comes down to the brass tacks, it's a tool and it has it's place - in your toolbox. Some would agree while others violently disagree, but it really is preference.

Keep in mind that when designing a project, using a singleton could be the wrong way of doing it in the long run, as it may limit your overall control. However, there are many patterns to choose from, and you can always mix-n-match.
Corry
user 17190121
Riverview, FL
Post #: 12
Thanks for the replies. I'll tuck this away in some corner of my mind for a while. Once I get a better grasp of how to use classes in real life, I'll have this tool sitting in my toolbox waiting for the right job to use it on.
Powered by mvnForum

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy