Php: singleton VS full static class? When use what?

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.

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.

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 at 19:15.

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.

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 at 19:26.

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.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 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 at 12:14.

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions