Using Singletones in PHP

I want to use the created Singleton DB I in several class methods. Is it more suitable to create a Singleton separately in each method or to create it through __constructor () and access it from a variable in each method?

Thanks.

+3
source share
2 answers

Given one singleton, the result will be the same: there will be only one instance of your database object, and it will always be the same.

So, both solutions will work and will give the same result; at least if your DB class is used every time an instance of your class is created, and, of course, the performance difference is likely to be negligible.


, singleton : singleton, ...

... Dependency Injection ( __construct, ), - , "Mock" DB .

: ?

+3

, , ? , ? , , , ; , ( , - , ).

0

Source: https://habr.com/ru/post/1724440/


All Articles