Answer the question
In order to leave comments, you need to log in
Statics VS Singleton?
Is there any benefit to using a Singleton object instead of static attributes and methods?
Singleton:
class One {
private $var;
private $instance;
/** Пропущена защита конструкторов, клонов и wakeup`ов. */
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;
}
}
One::Instance()->set(2);
echo One::Instance()->get();
class Two {
static private $var;
static public function Set ( $var ) {
self::$var = $var;
}
static public function Get () {
return self::$var;
}
}
Two::Set(2);
echo Two::Get();
Answer the question
In order to leave comments, you need to log in
The main advantage of objects, polymorphism, is:
We got some kind of object and work with it through a certain interface without thinking about its implementation. If the calling code needs to slip us another implementation, we don't care.
And where this object was obtained from, through Singleton or not, there are many similar objects in the system or it is one, we are not interested.
And with the second method, we are rigidly attached to the class.
function func($obj) {
$obj->method1();
$obj->medhod2();
}
If you need a bunch of functions, albeit under a common umbrella - statics.
If you need an object - a loner.
An important difference between them is the call of the constructor and destructor. For a singleton, both are called automatically.
Whereas in a bunch of static methods, you have to check in each method to see if a constructor has been called to achieve the same effect. Yes, you can get rid of this by implementing __CallStatic and adopting method naming rules so that they are never called in their pure form, but doesn't this seem like a crutch to you?
Despite the comment above, there is absolutely no difference (the challenge is different). As a result, only one instance of the entity you work with appears here and there, and + the singleton in memory will take a few extra kilobytes as an object. The main thing is that they do the same job.
If you do not feel the difference between them in practice, then use the method that is more convenient for you.
You can ignore how it works inside, there are usually many more important places where you should think about optimization.
But I'm on the contrary, I'm thinking how to get rid of Singletons and Static objects. There is an opinion that this is evil, because it is tantamount to global data.
PS I just came across a project on which Overengineering is written in big red letters, and Singletons are used a lot there - on business and not on business.
I also ran into this issue when I implemented the registry. So far I have settled on a static array and methods, because there are only 2 methods and 1 static array.
See how to use. Even if we forget about constructors / destructors and other “sugar”, then the singleton has another advantage - it can be passed as a function / method parameter, that is, to reduce code cohesion.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question