D
D
dev4002016-05-02 21:52:32
PHP
dev400, 2016-05-02 21:52:32

What's the point of a singleton when there is static?

After all, in any case, we get a global state. What is the point of using singleton?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-05-02
@dev400

After all, in any case, we get the global state

What about the global state? Right, bad. I have one small project for iOS nearby where the guys decided to hang out and made a singleton with hundreds of public properties. And the whole system works with this global state, producing side effects. Encapsulation? no, haven't heard.
Singelton is a pattern that forbids us to have two instances of the same object within a process. The easiest way is to use static. Singletons are useful when, for example, you have two threads, and each one wants to create an object, and it is vital for you that there is only one object in the system so that there are no errors.
In PHP, where the idea of ​​multi-threaded programming is not very popular, and the "dying" execution model thrives, there is no point in singletons at all. And they use them because... attention... they want to have global access to all sorts of stuff, including the organization of global state.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question