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?

3

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

@Darhazar: +1 finally someone who explained me in quite simple few points why singleton could be better than a static class.
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.

4 Comments

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();
there is no such thing as static class in php. only properties and methods can be declared as static.
@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 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.
-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.

1 Comment

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)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.