Static VS Singleton?

Is there any profit in using the Singleton object instead of static attributes and methods?

class One {
 private $var;
 private $instance;

 /** Omitted to protect the designers, clones and wakeup's. */

 static public function Instance () {
 if ( is_null( $this->instance ))
 $this->instance = new static;

 return $this->instance;

 public function set ( $var ) {
 $this->var = $var;

 public function get () {
 return $this->var;
echo One::Instance()->get();

class Two {
 static private $var;

 static public function Set ( $var ) {
 self::$var = $var;

 static public function Get () {
 return self::$var;
echo Two::Get();

I always use option "Two", but suddenly I had something important missing?
October 8th 19 at 01:04
7 answers
October 8th 19 at 01:06
The main advantage of objects, polimorphism on:
function func($obj) {<br> $obj->method1();<br> $obj->medhod2();<br> }
Got some object and work with it through a particular interface without worrying about its implementation. If the calling code will need to slip us a different implementation, not ours.

And from where you retrieved this object, using a Singleton or not, a lot of such objects in the system or you have one, we're not interested.
But with the second method, we rigidly attached to the class.
This can be solved as a succession of static - aniya.Kertzmann65 commented on October 8th 19 at 01:09
Yeah, and pass the class name :) - valli commented on October 8th 19 at 01:12
For nasledovaniya don't need the class name, always use the parent and all of any other heir will have its interface. Your K. O. - aniya.Kertzmann65 commented on October 8th 19 at 01:15
How can I specific place to call a static method of the class, not the name of the reference to this class and using only the parent? - valli commented on October 8th 19 at 01:18
And how do You call the unique method of the object using the interface of the parent? The purpose of the interface if You want to call a method which it is not? - aniya.Kertzmann65 commented on October 8th 19 at 01:21
Using the interface parent, I'll call the overridden implementation of the method (for objects). Having only the name of the base class I can only call the base implementation of a static method. - valli commented on October 8th 19 at 01:24
October 8th 19 at 01:08
If you need a bunch of functions, though under the General auspices of static.
If you need the object alone.

An important difference between them — the call to the constructor and destructor. Singleton is both invoked automatically.
While in the bunch of static methods you have in each method to verify whether it was caused by a designer to achieve the same effect. Yes, this can be eliminated by implementing the __CallStatic and adopting naming conventions of methods to pure form, they never called, but does it seem to you a crutch?
Yes, I agree, the constructor\destructor is an important distinction. When they used the crutches to come up with clearly makes no sense.

On the other hand I almost never use, because trying to use lazy-initialization of all that is possible, and to perform directly actions often nothing happens.
Logic and initialization still separate and check its status before each call.

If you need the object alone.

Not very aware of the semantic concept of "object" in this perspective. Let's say the register is the object?
Or a DB connection, which is almost always need in a single number? Technically Yes, the object state data and methods.
To hang they will be everywhere and DB::Query('...') for me simpatišnee than DB::Instance()->query('...'); - aniya.Kertzmann65 commented on October 8th 19 at 01:11
Technically, I reached the Tao of the PLO and under the object in the context of singleton understand the constructor and destructor.
Speaking specifically about the PHP static classes, in contrast to the singleton, do not know how __get, __set, reference to undeclared members leads not to the fatal error that may impose some inconvenience, if you want to dynamically access members. - valli commented on October 8th 19 at 01:14
The most correct answer. Even if the object has no initialization now, there is no guarantee that she won't need anyone ever and then. And the magic __callStatic here is evil, designed to replace the normal constructor. - aniya.Kertzmann65 commented on October 8th 19 at 01:17
October 8th 19 at 01:10
Despite kament above, no difference is absolutely (call different). In the end, and here and there appears only one entity instance with which You are working, and of the singleton in memory will take another few kilobytes in size as the object. The main thing that they are doing the same.
By the way, statics can't:
Singlton::getInstance()->add('qwe')->add('asd')->save(); - aniya.Kertzmann65 commented on October 8th 19 at 01:13
"kilobytes"? - valli commented on October 8th 19 at 01:16
Well, if we talk about the register for example, then calling Registry::DB() it is logical to obtain the object with which to work in chains.

The difference is actually Registry::Instance()->DB()->... or Registry::DB()->... - aniya.Kertzmann65 commented on October 8th 19 at 01:19
In the case of speakers you can replace the implementation samango Registry. And in the case of statics, you can't. For example when testing very can help. - valli commented on October 8th 19 at 01:22

For singleton it is also strange, because the object is not unique. - aniya.Kertzmann65 commented on October 8th 19 at 01:25
October 8th 19 at 01:12
If you don't feel the difference between them in practice, we use the method which you prefer.
How it is inside arranged, it is possible not to pay attention, usually there are many more important places where you should think about optimization.
October 8th 19 at 01:14
But I'm the opposite, I think how to get rid of Singletons and Static objects. There is an opinion that it was evil, because it is equivalent to global data.
PS Just got a project that written in big red letters Overengineering, and there are so many Singletons use — case and not the case.
Tell me at least one normal argument against 5 global variables (I do not think that a lot of singletons). Given that this (let's take static) variable names which I can nikogda do not overlap (You do not call a variable as a class name?). - aniya.Kertzmann65 commented on October 8th 19 at 01:17
In this project, 10 (ten!) singletons. And a few static classes. I usually do not mind the two global variables $user, $db. Everything else, in my opinion, too much. - valli commented on October 8th 19 at 01:20
usually not against the two global variables $user, $db.

I can assume, at the expense of db. But the user, for what? - aniya.Kertzmann65 commented on October 8th 19 at 01:23
$user is probably there because of the data authentication. - valli commented on October 8th 19 at 01:26
TheShock, in the context of normal use of the PHP user is usually global to the application because the ideology CGI "one request — one process" still remains, albeit somewhat disguised in the case of the server modules or FastCGI. Rather, databases can be several than users. - aniya.Kertzmann65 commented on October 8th 19 at 01:29
October 8th 19 at 01:16
Also ran into this issue when implemented by the registry. While stopped at a static array and methods, because there is only 2 methods and 1 static array.
October 8th 19 at 01:18
Depends on how you use. Even if you forget about konstruktory/destructors and other "sugar", then the singleton has another advantage — it can be passed as a parameter to a function/method, that is to reduce coupling of code.

Find more questions by tags PHPPatterns of designing