5

I understand that singleton enforces a class to be created once. But why should an instance exists if I dont access it directly? Why is that pattern for, isn't it easier just use full static class with static methods and datas?

deeped
  • 51
  • 1
  • 2
  • *(related)* [Who needs Singletons](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323) – Gordon Jul 26 '11 at 11:56
  • 2
    There are good language-agnostic answers here: http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern – Philip Jul 26 '11 at 11:57
  • 1
    *(suggested)* [Static considered harmful](http://kore-nordmann.de/blog/0103_static_considered_harmful.html) – Gordon Jul 26 '11 at 11:58

3 Answers3

7

Some time ago I was asked what is the benefit of using singleton over of using static class, here is my response:

  • Static class leads to invisible dependencies - that is a class that use the static class, but that class is not part of the class' interface. Singleton also allows this, because it provides global access point, but it's instance can be passed as an argument to the class / method
  • If there is any initialization, as the connect method, it should be called from each class method, which leads to duplicated code. On the other hand, the initialization of the singleton is performed in the constructor, which is called just once from the getInstance() method
  • Singleton can be easily refactored in a factory, adding a parameter to the getInstance() method and returning different instances
  • Static class is harder to extend, because if we want to override a method, that is called within the class with self::methodName(), we should override the caller as well (although in PHP 5.3 there is a late static binding, which can be used to avoid those problems)
  • If you need to add an argument, needed for all of the methods, you can easily do this in singleton because of the single access point, but you can't in a static class
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • @Darhazar: +1 finally someone who explained me in quite simple few points why singleton could be better than a static class. – Marco Demaio Aug 12 '11 at 19:26
5

The major difference between a static class and a singleton is that with the static class, you need to hardcode the class name in your code everywhere you use it:

StaticClass::doSomething();
StaticClass::doSomethingElse();

While with a singleton, you only need to hardcode the class name once:

$singleton = SingletonClass::getInstance();

// other code does not need to know where $singleton came from,
// or even that class SingletonClass exists at all:
$singleton->doSomething();
$singleton->doSomethingElse();

Another important difference is that singleton classes can be part of hierarchies and can implement interfaces.

This does not mean that Singleton (the pattern) is good and should be used liberally. But it is better than using a static class directly.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 1
    FYI the 1st reason you gave is not true anymore as of PHP 5.3 you could do: `$class = 'StaticClass'`, and then: `$class::doSomething(); $class::doSomethingElse();` – Marco Demaio Aug 12 '11 at 19:15
  • there is no such thing as static class in php. only properties and methods can be declared as static. – Radu May 19 '14 at 19:29
  • 1
    @Radu: A class with only static methods is for all intents and purposes a static class, even if the keyword cannot be applied to the class itself. You can also make a `final` class with only static methods -- perhaps it doesn't say "static" on the tin, but we would all agree that's as static a class as one can ever be. – Jon May 19 '14 at 21:08
  • @Jon the `final` keyword is ment to be used to prevent child classes from overriding methods or classes to be extended. If you chose to declare a method or a class as `final` it's your architectural decision. It has nothing to do with the jargon of calling a class that contains only static methods, `static`. – Radu May 19 '14 at 21:41
-1

[Edit]: The stuff I have written below is actually plain wrong. Just got alerted to this answer from years ago by a downvote. They do serve a purpose ;)

A singleton exists once, but it can have internal state - as opposed to a static class. You might e.g. use it as a global registry, which you can't do with a static class.

[Edit:] What comes next, though, is as true as it ever was.

It's debatable whether singletons are a good idea, though. They introduce global state into an application, which can make it very hard to test. But that is another discussion.

hashchange
  • 7,029
  • 1
  • 45
  • 41
  • 2
    You can most certainly use a static class as a global registry. What's stopping the static class from also having internal state (`private static` members)? – Jon Jul 26 '11 at 12:14